Skip to content

Instantly share code, notes, and snippets.

@williandricken williandricken/validateCPF.js Secret
Created Aug 24, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.