Skip to content

Instantly share code, notes, and snippets.

@brunoziie
Created July 3, 2014 19:27
Show Gist options
  • Save brunoziie/8bda79fe4154581fcbcf to your computer and use it in GitHub Desktop.
Save brunoziie/8bda79fe4154581fcbcf to your computer and use it in GitHub Desktop.
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