Created
December 28, 2013 21:14
-
-
Save carrerasrodrigo/8164290 to your computer and use it in GitHub Desktop.
This functions helps to verify if a CPF (brazilian document) is valid.
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 verify_cpf(cpf){ | |
/* This functions helps to verify if a CPF (brazilian document) | |
it's valid. Returns true if it is, false otherwise */ | |
cpf = cpf.replace(/\./g, "").replace(/\-/g, ""); | |
if (cpf.length != 11){ | |
return false; | |
} | |
var tt = 0, d =0; | |
for (var t=9; t>=9 && t<11; t++){ | |
d = 0; | |
for (var c=0; c<t; c++){ | |
d += parseInt(cpf.substring(c, c+1)) * (t + 1 - c); | |
tt = c; | |
} | |
d = ((10 * d) % 11) % 10; | |
if (parseInt(cpf.substring(tt+1, tt+2)) != d){ | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment