Skip to content

Addition Methods: New method nisBr (#2196) #2203

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

Closed
Closed
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
59 changes: 59 additions & 0 deletions src/additional/nisBR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Created for project jquery-validation.
* @Description Brazillian PIS or NIS number (Número de Identificação Social Pis ou Pasep) is the equivalent of a
* Brazilian tax registration number NIS of PIS numbers have 11 digits in total: 10 numbers followed by 1 check numbers
* that are being used for validation.
* @copyright (c) 25/10/2016 22:40, Cleiton da Silva Mendonça
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a copy-paste error. Can you please adjust the date to the right one.

* @author Cleiton da Silva Mendonça <cleiton.mendonca@gmail.com>
* @link http://gitlab.com/csmendonca Gitlab of Cleiton da Silva Mendonça
* @link http://github.com/csmendonca Github of Cleiton da Silva Mendonça
*/

$.validator.addMethod( "nisBR", function( value ) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this empty line.

var number;
var cn;
var sum = 0;
var dv;
var count;
var multiplier;

// Removing special characters from value
value = value.replace( /([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "" );

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

//Get check number of value
cn = parseInt( value.substring( 10, 11 ), 10 );

//Get number with 10 digits of the value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a space before this comment and the one above it in order to be consistent with the rest of the comments.

number = parseInt( value.substring( 0, 10 ), 10 );

for ( count = 2; count < 12; count++ ) {
multiplier = count;
if ( count === 10 ) {
multiplier = 2;
}
if ( count === 11 ) {
multiplier = 3;
}
sum += ( ( number % 10 ) * multiplier );
number = parseInt( number / 10, 10 );
}
dv = ( sum % 11 );

if ( dv > 1 ) {
dv = ( 11 - dv );
} else {
dv = 0;
}

if ( cn === dv ) {
return true;
} else {
return false;
}
}, "Please specify a valid NIS/PIS 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 @@ -72,5 +72,6 @@ $.extend( $.validator.messages, {
vinUS: "O n&uacute;mero de identifica&ccedil;&atilde;o de ve&iacute;culo informado (VIN) &eacute; inv&aacute;lido.",
zipcodeUS: "Por favor, forne&ccedil;a um c&oacute;digo postal americano v&aacute;lido.",
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."
cpfBR: "Por favor, forne&ccedil;a um CPF v&aacute;lido.",
nisBR: "Por favor, forne&ccedil;a um NIS/PIS v&aacute;lido"
} );
13 changes: 13 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,19 @@ QUnit.test( "cpfBR", function( assert ) {
assert.ok( !method( "11144477737" ), "Invalid CPF Number: 2nd check number failed" );
} );

QUnit.test( "nisBR", function( assert ) {
var method = methodTest( "nisBR" );
assert.ok( method( "10757995753" ), "Valid NIS/PIS Number" );
assert.ok( method( "107.57995.75-3" ), "Valid NIS/PIS Number" );
assert.ok( method( "107.579.957-53" ), "Valid NIS/PIS Number" );
assert.ok( method( "107-579-957-53" ), "Valid NIS/PIS Number" );
assert.ok( method( "107.579.957.5-3" ), "Valid NIS/PIS Number" );
assert.ok( !method( "99999999999" ), "Invalid NIS/PIS Number: dump data" );
assert.ok( !method( "1075799575" ), "Invalid NIS/PIS Number: < 11 digits" );
assert.ok( !method( "111444777355" ), "Invalid NIS/PIS Number: > 11 digits" );
assert.ok( !method( "10757995752" ), "Invalid NIS/PIS Number: check number failed" );
} );

QUnit.test( "file accept - image wildcard", function( assert ) {
var input = acceptFileDummyInput( "test.png", "image/png" ),
$form = $( "<form />" ),
Expand Down