Content-Length: 358696 | pFad | http://github.com/jquery-validation/jquery-validation/pull/2222/files

DC Additional: Included validation rule for CNPJ from Brazil by jcspader · Pull Request #2222 · jquery-validation/jquery-validation · GitHub
Skip to content

Additional: Included validation rule for CNPJ from Brazil #2222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/additional/cnpjBR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Brazillian value number (Cadastrado de Pessoas Juridica).
* value numbers have 14 digits in total: 12 numbers followed by 2 check numbers that are being used for validation.
*/
$.validator.addMethod( "cnpjBR", function( value ) {

// Removing no number
value = value.replace( /[^\d]+/g, "" );

// Checking value to have 14 digits only
if ( value.length !== 14 ) {
return false;
}

// Elimina values invalidos conhecidos
if ( value === "00000000000000" ||
value === "11111111111111" ||
value === "22222222222222" ||
value === "33333333333333" ||
value === "44444444444444" ||
value === "55555555555555" ||
value === "66666666666666" ||
value === "77777777777777" ||
value === "88888888888888" ||
value === "99999999999999" ) {
return false;
}

// Valida DVs
var tamanho = ( value.length - 2 );
var numeros = value.substring( 0, tamanho );
var digitos = value.substring( tamanho );
var soma = 0;
var pos = tamanho - 7;

for ( var i = tamanho; i >= 1; i-- ) {
soma += numeros.charAt( tamanho - i ) * pos--;
if ( pos < 2 ) {
pos = 9;
}
}

var resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

if ( resultado !== parseInt( digitos.charAt( 0 ), 10 ) ) {
return false;
}

tamanho = tamanho + 1;
numeros = value.substring( 0, tamanho );
soma = 0;
pos = tamanho - 7;

for ( var il = tamanho; il >= 1; il-- ) {
soma += numeros.charAt( tamanho - il ) * pos--;
if ( pos < 2 ) {
pos = 9;
}
}

resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

if ( resultado !== parseInt( digitos.charAt( 1 ), 10 ) ) {
return false;
}

return true;

}, "Please specify a CNPJ value number" );
3 changes: 2 additions & 1 deletion src/localization/messages_pt_BR.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ $.extend( $.validator.messages, {
ziprange: "O c&oacute;digo postal deve estar entre 902xx-xxxx e 905xx-xxxx",
cpfBR: "Por favor, forne&ccedil;a um CPF v&aacute;lido.",
nisBR: "Por favor, forne&ccedil;a um NIS/PIS v&aacute;lido",
cnhBR: "Por favor, forne&ccedil;a um CNH v&aacute;lido."
cnhBR: "Por favor, forne&ccedil;a um CNH v&aacute;lido.",
cnpjBR: "Por favor, forne&ccedil;a um CNPJ v&aacute;lido."
} );
20 changes: 20 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,26 @@ QUnit.test( "cpfBR", function( assert ) {
assert.ok( !method( "11144477737" ), "Invalid CPF Number: 2nd check number failed" );
} );

QUnit.test( "cnpjBR", function( assert ) {
var method = methodTest( "cnpjBR" );
assert.ok( method( "18517604000175" ), "Valid CNPJ Number" );
assert.ok( method( "18.517.604/0001-75" ), "Valid CNPJ Number" );
assert.ok( method( "06994660000111" ), "Valid CNPJ Number" );
assert.ok( method( "06.994.660/0001-11" ), "Valid CNPJ Number" );
assert.ok( !method( "00000000000000" ), "Invalid CNPJ Number: dump data" );
assert.ok( !method( "11111111111111" ), "Invalid CNPJ Number: dump data" );
assert.ok( !method( "22222222222222" ), "Invalid CNPJ Number: dump data" );
assert.ok( !method( "99999999999999" ), "Invalid CNPJ Number: dump data" );
assert.ok( !method( "8517604000175" ), "Invalid CNPJ Number: < 14 digits" );
assert.ok( !method( "8.517.604/0001-75" ), "Invalid CNPJ Number: < 14 digits" );
assert.ok( !method( "1185176040001750" ), "Invalid CNPJ Number: > 14 digits" );
assert.ok( !method( "18.517.604/0001-750" ), "Invalid CNPJ Number: > 14 digits" );
assert.ok( !method( "18517604000174" ), "Invalid CNPJ Number" );
assert.ok( !method( "18.517.604/0001-74" ), "Invalid CNPJ Number" );
assert.ok( !method( "06994660000211" ), "Invalid CNPJ Number" );
assert.ok( !method( "06.994.660/0002-11" ), "Invalid CNPJ Number" );
} );

QUnit.test( "nisBR", function( assert ) {
var method = methodTest( "nisBR" );
assert.ok( method( "10757995753" ), "Valid NIS/PIS Number" );
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/jquery-validation/jquery-validation/pull/2222/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy