Skip to content

Commit 743077c

Browse files
jcspaderArkni
authored andcommitted
Additional: Add Brazillian CNPJ validation rule (jquery-validation#2222)
1 parent fcf782e commit 743077c

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/additional/cnpjBR.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Brazillian value number (Cadastrado de Pessoas Juridica).
3+
* value numbers have 14 digits in total: 12 numbers followed by 2 check numbers that are being used for validation.
4+
*/
5+
$.validator.addMethod( "cnpjBR", function( value ) {
6+
7+
// Removing no number
8+
value = value.replace( /[^\d]+/g, "" );
9+
10+
// Checking value to have 14 digits only
11+
if ( value.length !== 14 ) {
12+
return false;
13+
}
14+
15+
// Elimina values invalidos conhecidos
16+
if ( value === "00000000000000" ||
17+
value === "11111111111111" ||
18+
value === "22222222222222" ||
19+
value === "33333333333333" ||
20+
value === "44444444444444" ||
21+
value === "55555555555555" ||
22+
value === "66666666666666" ||
23+
value === "77777777777777" ||
24+
value === "88888888888888" ||
25+
value === "99999999999999" ) {
26+
return false;
27+
}
28+
29+
// Valida DVs
30+
var tamanho = ( value.length - 2 );
31+
var numeros = value.substring( 0, tamanho );
32+
var digitos = value.substring( tamanho );
33+
var soma = 0;
34+
var pos = tamanho - 7;
35+
36+
for ( var i = tamanho; i >= 1; i-- ) {
37+
soma += numeros.charAt( tamanho - i ) * pos--;
38+
if ( pos < 2 ) {
39+
pos = 9;
40+
}
41+
}
42+
43+
var resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
44+
45+
if ( resultado !== parseInt( digitos.charAt( 0 ), 10 ) ) {
46+
return false;
47+
}
48+
49+
tamanho = tamanho + 1;
50+
numeros = value.substring( 0, tamanho );
51+
soma = 0;
52+
pos = tamanho - 7;
53+
54+
for ( var il = tamanho; il >= 1; il-- ) {
55+
soma += numeros.charAt( tamanho - il ) * pos--;
56+
if ( pos < 2 ) {
57+
pos = 9;
58+
}
59+
}
60+
61+
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
62+
63+
if ( resultado !== parseInt( digitos.charAt( 1 ), 10 ) ) {
64+
return false;
65+
}
66+
67+
return true;
68+
69+
}, "Please specify a CNPJ value number" );

src/localization/messages_pt_BR.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,6 @@ $.extend( $.validator.messages, {
7474
ziprange: "O c&oacute;digo postal deve estar entre 902xx-xxxx e 905xx-xxxx",
7575
cpfBR: "Por favor, forne&ccedil;a um CPF v&aacute;lido.",
7676
nisBR: "Por favor, forne&ccedil;a um NIS/PIS v&aacute;lido",
77-
cnhBR: "Por favor, forne&ccedil;a um CNH v&aacute;lido."
77+
cnhBR: "Por favor, forne&ccedil;a um CNH v&aacute;lido.",
78+
cnpjBR: "Por favor, forne&ccedil;a um CNPJ v&aacute;lido."
7879
} );

test/methods.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,26 @@ QUnit.test( "cpfBR", function( assert ) {
17171717
assert.ok( !method( "11144477737" ), "Invalid CPF Number: 2nd check number failed" );
17181718
} );
17191719

1720+
QUnit.test( "cnpjBR", function( assert ) {
1721+
var method = methodTest( "cnpjBR" );
1722+
assert.ok( method( "18517604000175" ), "Valid CNPJ Number" );
1723+
assert.ok( method( "18.517.604/0001-75" ), "Valid CNPJ Number" );
1724+
assert.ok( method( "06994660000111" ), "Valid CNPJ Number" );
1725+
assert.ok( method( "06.994.660/0001-11" ), "Valid CNPJ Number" );
1726+
assert.ok( !method( "00000000000000" ), "Invalid CNPJ Number: dump data" );
1727+
assert.ok( !method( "11111111111111" ), "Invalid CNPJ Number: dump data" );
1728+
assert.ok( !method( "22222222222222" ), "Invalid CNPJ Number: dump data" );
1729+
assert.ok( !method( "99999999999999" ), "Invalid CNPJ Number: dump data" );
1730+
assert.ok( !method( "8517604000175" ), "Invalid CNPJ Number: < 14 digits" );
1731+
assert.ok( !method( "8.517.604/0001-75" ), "Invalid CNPJ Number: < 14 digits" );
1732+
assert.ok( !method( "1185176040001750" ), "Invalid CNPJ Number: > 14 digits" );
1733+
assert.ok( !method( "18.517.604/0001-750" ), "Invalid CNPJ Number: > 14 digits" );
1734+
assert.ok( !method( "18517604000174" ), "Invalid CNPJ Number" );
1735+
assert.ok( !method( "18.517.604/0001-74" ), "Invalid CNPJ Number" );
1736+
assert.ok( !method( "06994660000211" ), "Invalid CNPJ Number" );
1737+
assert.ok( !method( "06.994.660/0002-11" ), "Invalid CNPJ Number" );
1738+
} );
1739+
17201740
QUnit.test( "nisBR", function( assert ) {
17211741
var method = methodTest( "nisBR" );
17221742
assert.ok( method( "10757995753" ), "Valid NIS/PIS Number" );

0 commit comments

Comments
 (0)