Skip to content

Commit 01ce49c

Browse files
cleitonmendoncaArkni
authored andcommitted
Additional: Add Brazillian PIS/NIS number validation method (jquery-validation#2204)
1 parent 5ad8de8 commit 01ce49c

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/additional/nisBR.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Created for project jquery-validation.
3+
* @Description Brazillian PIS or NIS number (Número de Identificação Social Pis ou Pasep) is the equivalent of a
4+
* Brazilian tax registration number NIS of PIS numbers have 11 digits in total: 10 numbers followed by 1 check numbers
5+
* that are being used for validation.
6+
* @copyright (c) 21/08/2018 13:14, Cleiton da Silva Mendonça
7+
* @author Cleiton da Silva Mendonça <cleiton.mendonca@gmail.com>
8+
* @link http://gitlab.com/csmendonca Gitlab of Cleiton da Silva Mendonça
9+
* @link http://github.com/csmendonca Github of Cleiton da Silva Mendonça
10+
*/
11+
$.validator.addMethod( "nisBR", function( value ) {
12+
var number;
13+
var cn;
14+
var sum = 0;
15+
var dv;
16+
var count;
17+
var multiplier;
18+
19+
// Removing special characters from value
20+
value = value.replace( /([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "" );
21+
22+
// Checking value to have 11 digits only
23+
if ( value.length !== 11 ) {
24+
return false;
25+
}
26+
27+
//Get check number of value
28+
cn = parseInt( value.substring( 10, 11 ), 10 );
29+
30+
//Get number with 10 digits of the value
31+
number = parseInt( value.substring( 0, 10 ), 10 );
32+
33+
for ( count = 2; count < 12; count++ ) {
34+
multiplier = count;
35+
if ( count === 10 ) {
36+
multiplier = 2;
37+
}
38+
if ( count === 11 ) {
39+
multiplier = 3;
40+
}
41+
sum += ( ( number % 10 ) * multiplier );
42+
number = parseInt( number / 10, 10 );
43+
}
44+
dv = ( sum % 11 );
45+
46+
if ( dv > 1 ) {
47+
dv = ( 11 - dv );
48+
} else {
49+
dv = 0;
50+
}
51+
52+
if ( cn === dv ) {
53+
return true;
54+
} else {
55+
return false;
56+
}
57+
}, "Please specify a valid NIS/PIS number" );

src/localization/messages_pt_BR.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@ $.extend( $.validator.messages, {
7272
vinUS: "O n&uacute;mero de identifica&ccedil;&atilde;o de ve&iacute;culo informado (VIN) &eacute; inv&aacute;lido.",
7373
zipcodeUS: "Por favor, forne&ccedil;a um c&oacute;digo postal americano v&aacute;lido.",
7474
ziprange: "O c&oacute;digo postal deve estar entre 902xx-xxxx e 905xx-xxxx",
75-
cpfBR: "Por favor, forne&ccedil;a um CPF v&aacute;lido."
75+
cpfBR: "Por favor, forne&ccedil;a um CPF v&aacute;lido.",
76+
nisBR: "Por favor, forne&ccedil;a um NIS/PIS v&aacute;lido"
7677
} );

test/methods.js

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

1720+
QUnit.test( "nisBR", function( assert ) {
1721+
var method = methodTest( "nisBR" );
1722+
assert.ok( method( "10757995753" ), "Valid NIS/PIS Number" );
1723+
assert.ok( method( "107.57995.75-3" ), "Valid NIS/PIS Number" );
1724+
assert.ok( method( "107.579.957-53" ), "Valid NIS/PIS Number" );
1725+
assert.ok( method( "107-579-957-53" ), "Valid NIS/PIS Number" );
1726+
assert.ok( method( "107.579.957.5-3" ), "Valid NIS/PIS Number" );
1727+
assert.ok( !method( "99999999999" ), "Invalid NIS/PIS Number: dump data" );
1728+
assert.ok( !method( "1075799575" ), "Invalid NIS/PIS Number: < 11 digits" );
1729+
assert.ok( !method( "111444777355" ), "Invalid NIS/PIS Number: > 11 digits" );
1730+
assert.ok( !method( "10757995752" ), "Invalid NIS/PIS Number: check number failed" );
1731+
} );
1732+
17201733
QUnit.test( "file accept - image wildcard", function( assert ) {
17211734
var input = acceptFileDummyInput( "test.png", "image/png" ),
17221735
$form = $( "<form />" ),

0 commit comments

Comments
 (0)