Skip to content

Instantly share code, notes, and snippets.

@luscas
Created April 24, 2019 19:54
Show Gist options
  • Save luscas/9a6197ff37f3ed9ca776f0ac257213c5 to your computer and use it in GitHub Desktop.
Save luscas/9a6197ff37f3ed9ca776f0ac257213c5 to your computer and use it in GitHub Desktop.
Validar CPF
function validate_cpf(cpf){
cpf = cpf.replace(/\D/g, '');
if(cpf.toString().length != 11 || /^(\d)\1{10}$/.test(cpf)) return false;
var result = true;
[9,10].forEach(function(j){
var soma = 0, r;
cpf.split(/(?=)/).splice(0,j).forEach(function(e, i){
soma += parseInt(e) * ((j+2)-(i+1));
});
r = soma % 11;
r = (r <2)?0:11-r;
if(r != cpf.substring(j, j+1)) result = false;
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment