Skip to content

Instantly share code, notes, and snippets.

@fgrehm fgrehm/gist:3744111
Created Sep 18, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.