-
-
Save gutierri/9898876 to your computer and use it in GitHub Desktop.
Validação de CPF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validate_cpf(cpf) { | |
var sum, summod11; | |
cpf = cpf.replace(/[^0-9]/g, ''); | |
if(cpf.length != 11 || cpf.match(/^([0-9])\1+$/)) { | |
return false; | |
} | |
// 9 primeiros digitos do cpf | |
var digit = Array.prototype.slice.call(cpf, 0, 9); | |
// calculo dos 2 digitos verificadores | |
for(var j=10; j <= 11; j++){ | |
sum = 0; | |
for(i=0; i< j-1; i++) { | |
sum += (j-i) * parseInt(digit[i], 10); | |
} | |
summod11 = sum % 11; | |
digit[j-1] = summod11 < 2 ? 0 : 11 - summod11; | |
} | |
if(digit[9] == parseInt(cpf[9], 10) && digit[10] == parseInt(cpf[10], 10) ) { | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment