-
-
Save williandricken/2df1a88ff9c6e14fb7f4937717a08e98 to your computer and use it in GitHub Desktop.
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 validateCPF(cpf) | |
{ | |
var number, digits, sum, i, result, equal_digits; | |
equal_digits = 1; | |
if (cpf.length < 11) | |
return false; | |
for (i = 0; i < cpf.length - 1; i++) | |
if (cpf.charAt(i) != cpf.charAt(i + 1)) | |
{ | |
equal_digits = 0; | |
break; | |
} | |
if (!equal_digits) | |
{ | |
number = cpf.substring(0,9); | |
digits = cpf.substring(9); | |
sum = 0; | |
for (i = 10; i > 1; i--) | |
sum += number.charAt(10 - i) * i; | |
result = sum % 11 < 2 ? 0 : 11 - sum % 11; | |
if (result != digits.charAt(0)) | |
return false; | |
number = cpf.substring(0,10); | |
sum = 0; | |
for (i = 11; i > 1; i--) | |
sum += number.charAt(11 - i) * i; | |
result = sum % 11 < 2 ? 0 : 11 - sum % 11; | |
if (result != digits.charAt(1)) | |
return false; | |
return true; | |
} | |
else | |
return false; | |
} | |
console.log(validateCPF('85351346893')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment