Created
April 24, 2019 19:54
-
-
Save luscas/9a6197ff37f3ed9ca776f0ac257213c5 to your computer and use it in GitHub Desktop.
Validar 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){ | |
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