Skip to content

Commit 317c20f

Browse files
Alfonso Martínnschonni
authored andcommitted
Added NIF, NIE and CIF Spanish documents numbers validation
* Added some translations to /localization * Added test suite Fixes jquery-validationgh-830
1 parent 519bbc6 commit 317c20f

File tree

11 files changed

+241
-6
lines changed

11 files changed

+241
-6
lines changed

src/additional/cifES.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Código de identificación fiscal ( CIF ) is the tax identification code for Spanish legal entities
3+
* Further rules can be found in Spanish on http://es.wikipedia.org/wiki/C%C3%B3digo_de_identificaci%C3%B3n_fiscal
4+
*/
5+
jQuery.validator.addMethod( "cifES", function ( value, element ) {
6+
"use strict";
7+
8+
var sum,
9+
num = [],
10+
controlDigit;
11+
12+
value = value.toUpperCase();
13+
14+
// Quick format test
15+
if ( !value.match( '((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)' ) ) {
16+
return false;
17+
}
18+
19+
for ( var i = 0; i < 9; i++ ) {
20+
num[ i ] = parseInt( value.charAt( i ), 10 );
21+
}
22+
23+
// Algorithm for checking CIF codes
24+
sum = num[ 2 ] + num[ 4 ] + num[ 6 ];
25+
for ( var count = 1; count < 8; count += 2 ) {
26+
var tmp = ( 2 * num[ count ] ).toString(),
27+
secondDigit = tmp.charAt( 1 );
28+
29+
sum += parseInt( tmp.charAt( 0 ), 10 ) + ( secondDigit === '' ? 0 : parseInt( secondDigit, 10 ) );
30+
}
31+
32+
/* The first (position 1) is a letter following the following criteria:
33+
* A. Corporations
34+
* B. LLCs
35+
* C. General partnerships
36+
* D. Companies limited partnerships
37+
* E. Communities of goods
38+
* F. Cooperative Societies
39+
* G. Associations
40+
* H. Communities of homeowners in horizontal property regime
41+
* J. Civil Societies
42+
* K. Old format
43+
* L. Old format
44+
* M. Old format
45+
* N. Nonresident entities
46+
* P. Local authorities
47+
* Q. Autonomous bodies, state or not, and the like, and congregations and religious institutions
48+
* R. Congregations and religious institutions (since 2008 ORDER EHA/451/2008)
49+
* S. Organs of State Administration and regions
50+
* V. Agrarian Transformation
51+
* W. Permanent establishments of non-resident in Spain
52+
*/
53+
if ( /^[ABCDEFGHJNPQRSUVW]{1}/.test( value ) ) {
54+
sum += '';
55+
controlDigit = 10 - parseInt( sum.charAt( sum.length - 1 ), 10 );
56+
value += controlDigit;
57+
return ( num[ 8 ].toString() === String.fromCharCode( 64 + controlDigit ) || num[ 8 ].toString() === value.charAt( value.length - 1 ) );
58+
}
59+
60+
return false;
61+
62+
}, "Please specify a valid CIF number." );

src/additional/nieES.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* The número de identidad de extranjero ( NIE )is a code used to identify the non-nationals in Spain
3+
*/
4+
jQuery.validator.addMethod( "nieES", function ( value, element ) {
5+
"use strict";
6+
7+
value = value.toUpperCase();
8+
9+
// Basic format test
10+
if ( !value.match( '((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)' ) ) {
11+
return false;
12+
}
13+
14+
// Test NIE
15+
//T
16+
if ( /^[T]{1}/.test( value ) ) {
17+
return ( value[ 8 ] === /^[T]{1}[A-Z0-9]{8}$/.test( value ) );
18+
}
19+
20+
//XYZ
21+
if ( /^[XYZ]{1}/.test( value ) ) {
22+
return (
23+
value[ 8 ] === "TRWAGMYFPDXBNJZSQVHLCKE".charAt(
24+
value.replace( 'X', '0' )
25+
.replace( 'Y', '1' )
26+
.replace( 'Z', '2' )
27+
.substring( 0, 8 ) % 23
28+
)
29+
);
30+
}
31+
32+
return false;
33+
34+
}, "Please specify a valid NIE number." );
35+

src/additional/nifES.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* The Número de Identificación Fiscal ( NIF ) is the way tax identification used in Spain for individuals
3+
*/
4+
jQuery.validator.addMethod( "nifES", function ( value, element ) {
5+
"use strict";
6+
7+
value = value.toUpperCase();
8+
9+
// Basic format test
10+
if ( !value.match('((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)') ) {
11+
return false;
12+
}
13+
14+
// Test NIF
15+
if ( /^[0-9]{8}[A-Z]{1}$/.test( value ) ) {
16+
return ( "TRWAGMYFPDXBNJZSQVHLCKE".charAt( value.substring( 8, 0 ) % 23 ) === value.charAt( 8 ) );
17+
}
18+
// Test specials NIF (starts with K, L or M)
19+
if ( /^[KLM]{1}/.test( value ) ) {
20+
return ( value[ 8 ] === String.fromCharCode( 64 ) );
21+
}
22+
23+
return false;
24+
25+
}, "Please specify a valid NIF number." );

src/localization/messages_es.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
rangelength: $.validator.format("Por favor, escribe un valor entre {0} y {1} caracteres."),
2121
range: $.validator.format("Por favor, escribe un valor entre {0} y {1}."),
2222
max: $.validator.format("Por favor, escribe un valor menor o igual a {0}."),
23-
min: $.validator.format("Por favor, escribe un valor mayor o igual a {0}.")
23+
min: $.validator.format("Por favor, escribe un valor mayor o igual a {0}."),
24+
nifES: "Por favor, escribe un NIF válido.",
25+
nieES: "Por favor, escribe un NIE válido.",
26+
cifES: "Por favor, escribe un CIF válido."
2427
});
2528
}(jQuery));

src/localization/messages_es_AR.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
rangelength: $.validator.format("Por favor, escribí un valor entre {0} y {1} caracteres."),
2222
range: $.validator.format("Por favor, escribí un valor entre {0} y {1}."),
2323
max: $.validator.format("Por favor, escribí un valor menor o igual a {0}."),
24-
min: $.validator.format("Por favor, escribí un valor mayor o igual a {0}.")
24+
min: $.validator.format("Por favor, escribí un valor mayor o igual a {0}."),
25+
nifES: "Por favor, escribí un NIF válido.",
26+
nieES: "Por favor, escribí un NIE válido.",
27+
cifES: "Por favor, escribí un CIF válido."
2528
});
2629
}(jQuery));

src/localization/messages_fr.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
creditcardtypes: "Veuillez fournir un numéro de carte de crédit valide.",
4343
ipv4: "Veuillez fournir une adresse IP v4 valide.",
4444
ipv6: "Veuillez fournir une adresse IP v6 valide.",
45-
require_from_group: "Veuillez fournir au moins {0} de ces champs."
45+
require_from_group: "Veuillez fournir au moins {0} de ces champs.",
46+
nifES: "Veuillez fournir un numéro NIF valide.",
47+
nieES: "Veuillez fournir un numéro NIE valide.",
48+
cifES: "Veuillez fournir un numéro CIF valide."
4649
});
4750
}(jQuery));

src/localization/messages_it.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
rangelength: $.validator.format("Inserisci un valore compreso tra {0} e {1} caratteri."),
2121
range: $.validator.format("Inserisci un valore compreso tra {0} e {1}."),
2222
max: $.validator.format("Inserisci un valore minore o uguale a {0}."),
23-
min: $.validator.format("Inserisci un valore maggiore o uguale a {0}.")
23+
min: $.validator.format("Inserisci un valore maggiore o uguale a {0}."),
24+
nifES: "Inserisci un NIF valido.",
25+
nieES: "Inserisci un NIE valido.",
26+
cifES: "Inserisci un CIF valido."
2427
});
2528
}(jQuery));

src/localization/messages_pt_BR.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
rangelength: $.validator.format("Por favor, forne&ccedil;a um valor entre {0} e {1} caracteres de comprimento."),
2222
range: $.validator.format("Por favor, forne&ccedil;a um valor entre {0} e {1}."),
2323
max: $.validator.format("Por favor, forne&ccedil;a um valor menor ou igual a {0}."),
24-
min: $.validator.format("Por favor, forne&ccedil;a um valor maior ou igual a {0}.")
24+
min: $.validator.format("Por favor, forne&ccedil;a um valor maior ou igual a {0}."),
25+
nifES: "Por favor, forne&ccedil;a um NIF v&aacute;lido.",
26+
nieES: "Por favor, forne&ccedil;a um NIE v&aacute;lido.",
27+
cifEE: "Por favor, forne&ccedil;a um CIF v&aacute;lido."
2528
});
2629
}(jQuery));

src/localization/messages_pt_PT.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
rangelength: $.validator.format("Por favor, introduza entre {0} e {1} caracteres."),
2222
range: $.validator.format("Por favor, introduza um valor entre {0} e {1}."),
2323
max: $.validator.format("Por favor, introduza um valor menor ou igual a {0}."),
24-
min: $.validator.format("Por favor, introduza um valor maior ou igual a {0}.")
24+
min: $.validator.format("Por favor, introduza um valor maior ou igual a {0}."),
25+
nifES: "Por favor, introduza um NIF v&aacute;lido.",
26+
nieES: "Por favor, introduza um NIE v&aacute;lido.",
27+
cifES: "Por favor, introduza um CIF v&aacute;lido."
2528
});
2629
}(jQuery));

test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!DOCTYPE html>
22
<html id="html">
33
<head>
4+
<meta charset="utf8" />
45
<title>jQuery - Validation Test Suite</title>
56
<link rel="stylesheet" href="qunit/qunit.css" />
67
<script src="jquery.js"></script>

0 commit comments

Comments
 (0)