Skip to content

Commit 31176a2

Browse files
krzywieckistaabm
authored andcommitted
Additional: Polish tax id validation method (jquery-validation#1850)
1 parent 6781d94 commit 31176a2

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/additional/nipPL.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Numer identyfikacji podatkowej ( NIP ) is the way tax identification used in Poland for companies
3+
*/
4+
$.validator.addMethod( "nipPL", function( value ) {
5+
"use strict";
6+
7+
value = value.replace( /[^0-9]/g, "" );
8+
9+
if ( value.length !== 10 ) {
10+
return false;
11+
}
12+
13+
var arrSteps = [ 6, 5, 7, 2, 3, 4, 5, 6, 7 ];
14+
var intSum = 0;
15+
for ( var i = 0; i < 9; i++ ) {
16+
intSum += arrSteps[ i ] * value[ i ];
17+
}
18+
var int2 = intSum % 11;
19+
var intControlNr = ( int2 === 10 ) ? 0 : int2;
20+
21+
return ( intControlNr === parseInt( value[ 9 ], 10 ) );
22+
}, "Please specify a valid NIP number." );

src/localization/messages_pl.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $.extend( $.validator.messages, {
1414
creditcard: "Proszę o podanie prawidłowej karty kredytowej.",
1515
equalTo: "Proszę o podanie tej samej wartości ponownie.",
1616
extension: "Proszę o podanie wartości z prawidłowym rozszerzeniem.",
17+
nipPL: "Proszę o podanie prawidłowego numeru NIP.",
1718
maxlength: $.validator.format( "Proszę o podanie nie więcej niż {0} znaków." ),
1819
minlength: $.validator.format( "Proszę o podanie przynajmniej {0} znaków." ),
1920
rangelength: $.validator.format( "Proszę o podanie wartości o długości od {0} do {1} znaków." ),

test/methods.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,34 @@ QUnit.test( "cifES", function( assert ) {
14951495
assert.ok( !method( "B-43.522.192" ), "CIF invalid: dots and dash" );
14961496
} );
14971497

1498+
QUnit.test( "nipPL", function( assert ) {
1499+
var method = methodTest( "nipPL" );
1500+
assert.ok( method( "3514242002" ), "NIP valid" );
1501+
assert.ok( method( "8117892840" ), "NIP valid" );
1502+
assert.ok( method( "7249598309" ), "NIP valid" );
1503+
assert.ok( method( "6853539166" ), "NIP valid" );
1504+
assert.ok( method( "5715750580" ), "NIP valid" );
1505+
assert.ok( method( "3496120813" ), "NIP valid" );
1506+
assert.ok( method( "1565710251" ), "NIP valid" );
1507+
assert.ok( method( "8190761165" ), "NIP valid" );
1508+
assert.ok( method( "9487499667" ), "NIP valid" );
1509+
assert.ok( method( "9283384684" ), "NIP valid" );
1510+
assert.ok( method( "3887569138" ), "NIP valid" );
1511+
assert.ok( method( "3962898856" ), "NIP valid" );
1512+
assert.ok( !method( "76355753" ), "NIP invalid: too short" );
1513+
assert.ok( !method( "454" ), "NIP invalid: too short" );
1514+
assert.ok( !method( "234565545" ), "NIP invalid: too short" );
1515+
assert.ok( !method( "543455" ), "NIP invalid: too short" );
1516+
assert.ok( !method( "6345634563456" ), "NIP invalid: too long" );
1517+
assert.ok( !method( "53453453455335" ), "NIP invalid: too long" );
1518+
assert.ok( !method( "543453760902" ), "NIP invalid: too long" );
1519+
assert.ok( !method( "43090012454" ), "NIP invalid: too long" );
1520+
assert.ok( !method( "3958250194" ), "NIP invalid: wrong checksum" );
1521+
assert.ok( !method( "3928541049" ), "NIP invalid: wrong checksum" );
1522+
assert.ok( !method( "5920397295" ), "NIP invalid: wrong checksum" );
1523+
assert.ok( !method( "9502947712" ), "NIP invalid: wrong checksum" );
1524+
} );
1525+
14981526
QUnit.test( "maxWords", function( assert ) {
14991527
var method = methodTest( "maxWords" ),
15001528
maxWords = 6;

0 commit comments

Comments
 (0)