Skip to content

Instantly share code, notes, and snippets.

@felipekm
Created November 24, 2015 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipekm/494f2a981ad83d9bcd96 to your computer and use it in GitHub Desktop.
Save felipekm/494f2a981ad83d9bcd96 to your computer and use it in GitHub Desktop.
validaCpf
function VerificaCPF(strCpf) {
if (!strCpf || strCpf === '') {
return false;
}
var soma = 0,
resto;
for (i = 1; i <= 9; i += 1) {
soma = soma + parseInt(strCpf.substring(i - 1, i)) * (11 - i);
}
resto = soma % 11;
if (resto == 10 || resto == 11 || resto < 2) {
resto = 0;
} else {
resto = 11 - resto;
}
if (resto != parseInt(strCpf.substring(9, 10))) {
return false;
}
soma = 0;
for (i = 1; i <= 10; i++) {
soma = soma + parseInt(strCpf.substring(i - 1, i)) * (12 - i);
}
resto = soma % 11;
if (resto == 10 || resto == 11 || resto < 2) {
resto = 0;
} else {
resto = 11 - resto;
}
if (resto != parseInt(strCpf.substring(10, 11))) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment