- Posts: 44
- Thank you received: 0
validação com java script
3 years 5 months ago #125749
by adridg
validação com java script was created by adridg
Fiz uma questão para validar cpf.
A questão é numérica e aceita até 11 caracters.
Ai inseri (atraves do botão código fonte), o script abaixo, mas não está dando certo.
Não está fazendo nenhum tipo de validaçaõ no campo
Agradeço se alguem puder ajudar.
Uso LS Versão 2.06+ Build 150831
<script type="text/javascript" charset="utf-8">
function validarCPF(cpf) {
cpf = cpf.replace(/[^\d]+/g,'');
if(cpf == '') return false;
// Elimina CPFs invalidos conhecidos
if (cpf.length != 11 ||
cpf == "00000000000" ||
cpf == "11111111111" ||
cpf == "22222222222" ||
cpf == "33333333333" ||
cpf == "44444444444" ||
cpf == "55555555555" ||
cpf == "66666666666" ||
cpf == "77777777777" ||
cpf == "88888888888" ||
cpf == "99999999999")
return false;
// Valida 1o digito
add = 0;
for (i=0; i < 9; i ++)
add += parseInt(cpf.charAt(i)) * (10 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
// Valida 2o digito
add = 0;
for (i = 0; i < 10; i ++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
return true;
}
</script>
A questão é numérica e aceita até 11 caracters.
Ai inseri (atraves do botão código fonte), o script abaixo, mas não está dando certo.
Não está fazendo nenhum tipo de validaçaõ no campo
Agradeço se alguem puder ajudar.
Uso LS Versão 2.06+ Build 150831
<script type="text/javascript" charset="utf-8">
function validarCPF(cpf) {
cpf = cpf.replace(/[^\d]+/g,'');
if(cpf == '') return false;
// Elimina CPFs invalidos conhecidos
if (cpf.length != 11 ||
cpf == "00000000000" ||
cpf == "11111111111" ||
cpf == "22222222222" ||
cpf == "33333333333" ||
cpf == "44444444444" ||
cpf == "55555555555" ||
cpf == "66666666666" ||
cpf == "77777777777" ||
cpf == "88888888888" ||
cpf == "99999999999")
return false;
// Valida 1o digito
add = 0;
for (i=0; i < 9; i ++)
add += parseInt(cpf.charAt(i)) * (10 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
// Valida 2o digito
add = 0;
for (i = 0; i < 10; i ++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
return true;
}
</script>
Please Log in or Create an account to join the conversation.
3 years 5 months ago - 3 years 5 months ago #125754
by holch
I'm not a LimeSurvey GmbH member. I answer at the LimeSurvey forum in my spare time. No support via private message.
Some helpful links: Manual (EN) | Question Types | Workarounds
Replied by holch on topic validação com java script
Por favor, tomar cuidado em que forum postam. Isso aqui é claramente português e o forum é em Ingles.
Então tem duas opções: postar aqui em Ingles ou postar no forum em Portugues.
Vou mudar o forum deste topico.
Então tem duas opções: postar aqui em Ingles ou postar no forum em Portugues.
Vou mudar o forum deste topico.
I'm not a LimeSurvey GmbH member. I answer at the LimeSurvey forum in my spare time. No support via private message.
Some helpful links: Manual (EN) | Question Types | Workarounds
Last edit: 3 years 5 months ago by holch.
Please Log in or Create an account to join the conversation.