Skip to content

Instantly share code, notes, and snippets.

@fgrehm
Created September 18, 2012 16:28
Show Gist options
  • Save fgrehm/3744111 to your computer and use it in GitHub Desktop.
Save fgrehm/3744111 to your computer and use it in GitHub Desktop.
Validar CNPJ
window.validarCnpj = (cnpj) ->
cnpj = cnpj.replace(/\/|\.|-/g,"")
allEqual = /^(.)\1+$/
return if (cnpj.length < 14) || allEqual.test(cnpj)
a = []
b = 0
c = [6,5,4,3,2,9,8,7,6,5,4,3,2]
for i in [0..11]
a[i] = cnpj.charAt(i)
b += a[i] * c[i+1]
a[12] = if (x = (b % 11)) < 2 then 0 else (11 - x)
b = 0
for y in [0..12]
b += (a[y] * c[y])
a[13] = if ((x = b % 11) < 2) then 0 else (11 - x)
(cnpj.charAt(12) == a[12].toString()) && (cnpj.charAt(13) == a[13].toString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment