Skip to content

Instantly share code, notes, and snippets.

@robertopc
Last active September 13, 2022 20:42
Show Gist options
  • Save robertopc/ec1f8459662869d728c9e7253ec61102 to your computer and use it in GitHub Desktop.
Save robertopc/ec1f8459662869d728c9e7253ec61102 to your computer and use it in GitHub Desktop.
Validação de CPF Javascript
//valida o CPF digitado
function validarCPF(cpf){
cpf = cpf.toString().replace(/\.|\-/g, '' );
if(cpf.match(/^(1+|2+|3+|4+|5+|6+|7+|8+|9+|0+)$/) || cpf.length != 11)
return false;
soma = 0;
for(i = 0; i < 9; i ++) {
soma += parseInt(cpf.charAt(i)) * (10 - i);
}
resto = 11 - (soma % 11);
if(resto == 10 || resto == 11) {
resto = 0;
}
if(resto != parseInt(cpf.charAt(9))) {
return false;
}
soma = 0;
for (i = 0; i < 10; i ++) {
soma += parseInt(cpf.charAt(i)) * (11 - i);
}
resto = 11 - (soma % 11);
if(resto == 10 || resto == 11) {
resto = 0;
}
if(resto != parseInt(cpf.charAt(10))) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment