Created
July 3, 2014 19:27
-
-
Save brunoziie/8bda79fe4154581fcbcf 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
validates_cpf = (cpf) -> | |
cpf = $.trim(cpf).replace(/\.|\-/g,'').split('') | |
# calculate first digit | |
sum = 0 | |
i = 10 | |
$.each(cpf.slice(0,9), -> | |
sum += parseInt(this)*i | |
i -= 1 | |
) | |
result = sum % 11 | |
if result < 2 | |
result = 0 | |
else | |
result = 11 - result | |
if result != parseInt(cpf[9]) | |
return false | |
# calculate second digit | |
sum = 0 | |
i = 11 | |
$.each(cpf.slice(0,10), -> | |
sum += parseInt(this)*i | |
i -= 1 | |
) | |
result = sum % 11 | |
if result < 2 | |
result = 0 | |
else | |
result = 11 - result | |
result == parseInt(cpf[10]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment