Skip to content

Instantly share code, notes, and snippets.

@webarthur
Last active November 30, 2020 13:20
Show Gist options
  • Save webarthur/f33524533d26883bb9d2cda7ee442fb7 to your computer and use it in GitHub Desktop.
Save webarthur/f33524533d26883bb9d2cda7ee442fb7 to your computer and use it in GitHub Desktop.
Valida CNPJ - função em JavaScript
function is_cnpj(c) {
var b = [6,5,4,3,2,9,8,7,6,5,4,3,2];
if((c = c.replace(/[^\d]/g,"")).length != 14)
return false;
if(/0{14}/.test(c))
return false;
for (var i = 0, n = 0; i < 12; n += c[i] * b[++i]);
if(c[12] != (((n %= 11) < 2) ? 0 : 11 - n))
return false;
for (var i = 0, n = 0; i <= 12; n += c[i] * b[i++]);
if(c[13] != (((n %= 11) < 2) ? 0 : 11 - n))
return false;
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment