mirror of
https://github.com/jquery-validation/jquery-validation.git
synced 2025-12-14 20:35:47 +01:00
Additional: Add Brazillian PIS/NIS number validation method (#2204)
This commit is contained in:
committed by
Brahim Arkni
parent
5ad8de8948
commit
01ce49cf36
57
src/additional/nisBR.js
Normal file
57
src/additional/nisBR.js
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 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) 21/08/2018 13:14, Cleiton da Silva Mendonça
|
||||
* @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 ) {
|
||||
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
|
||||
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" );
|
||||
@@ -72,5 +72,6 @@ $.extend( $.validator.messages, {
|
||||
vinUS: "O número de identificação de veículo informado (VIN) é inválido.",
|
||||
zipcodeUS: "Por favor, forneça um código postal americano válido.",
|
||||
ziprange: "O código postal deve estar entre 902xx-xxxx e 905xx-xxxx",
|
||||
cpfBR: "Por favor, forneça um CPF válido."
|
||||
cpfBR: "Por favor, forneça um CPF válido.",
|
||||
nisBR: "Por favor, forneça um NIS/PIS válido"
|
||||
} );
|
||||
|
||||
@@ -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 />" ),
|
||||
|
||||
Reference in New Issue
Block a user