Skip to content

Instantly share code, notes, and snippets.

@nicolasaigner
Created November 4, 2016 23:47
Show Gist options
  • Select an option

  • Save nicolasaigner/7e61c10fc6134997c00b9ac2b223e599 to your computer and use it in GitHub Desktop.

Select an option

Save nicolasaigner/7e61c10fc6134997c00b9ac2b223e599 to your computer and use it in GitHub Desktop.
<html>
<head>
<script language="javascript">
function VerificaCPF () {
if (vercpf(document.form1.cpf.value)){
document.form1.submit();
}else{
errors="1";
if (errors) alert('CPF NÃO INVÁLIDO!');
document.retorno = (errors == '');
if(document.value != "true"){
document.form1.cpf.focus();
}
}
}
function vercpf (cpf){
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;
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;
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;
if(rev == parseInt(cpf.charAt(10))){
alert('CPF VÁLIDO!');
return true.document.form1.proximo.focus();
}
}
</script>
</head>
<body>
<form name="form1" method="post" onsubmit="VerificaCPF();">
<div>
Informe o CPF:
<input type="text" id="cpf" size="12" maxlength="11" placeholder="CPF" autofocus=""><br>
<div id="proximo" for="proximo2">
<input type="text" name="proximo2" id="proximo2" size="12" maxlength="11" placeholder="Próximo" style="margin-left: 104px;"><br>
</div>
<input type="button" name="Submit" value="Validar CPF" onclick="VerificaCPF();">
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment