| 2 |
lars |
1 |
/*
|
|
|
2 |
* The NIE (Número de Identificación de Extranjero) is a Spanish tax identification number assigned by the Spanish
|
|
|
3 |
* authorities to any foreigner.
|
|
|
4 |
*
|
|
|
5 |
* The NIE is the equivalent of a Spaniards Número de Identificación Fiscal (NIF) which serves as a fiscal
|
|
|
6 |
* identification number. The CIF number (Certificado de Identificación Fiscal) is equivalent to the NIF, but applies to
|
|
|
7 |
* companies rather than individuals. The NIE consists of an 'X' or 'Y' followed by 7 or 8 digits then another letter.
|
|
|
8 |
*/
|
|
|
9 |
$.validator.addMethod( "nieES", function( value, element ) {
|
|
|
10 |
"use strict";
|
|
|
11 |
|
|
|
12 |
if ( this.optional( element ) ) {
|
|
|
13 |
return true;
|
|
|
14 |
}
|
|
|
15 |
|
|
|
16 |
var nieRegEx = new RegExp( /^[MXYZ]{1}[0-9]{7,8}[TRWAGMYFPDXBNJZSQVHLCKET]{1}$/gi );
|
|
|
17 |
var validChars = "TRWAGMYFPDXBNJZSQVHLCKET",
|
|
|
18 |
letter = value.substr( value.length - 1 ).toUpperCase(),
|
|
|
19 |
number;
|
|
|
20 |
|
|
|
21 |
value = value.toString().toUpperCase();
|
|
|
22 |
|
|
|
23 |
// Quick format test
|
|
|
24 |
if ( value.length > 10 || value.length < 9 || !nieRegEx.test( value ) ) {
|
|
|
25 |
return false;
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
// X means same number
|
|
|
29 |
// Y means number + 10000000
|
|
|
30 |
// Z means number + 20000000
|
|
|
31 |
value = value.replace( /^[X]/, "0" )
|
|
|
32 |
.replace( /^[Y]/, "1" )
|
|
|
33 |
.replace( /^[Z]/, "2" );
|
|
|
34 |
|
|
|
35 |
number = value.length === 9 ? value.substr( 0, 8 ) : value.substr( 0, 9 );
|
|
|
36 |
|
|
|
37 |
return validChars.charAt( parseInt( number, 10 ) % 23 ) === letter;
|
|
|
38 |
|
|
|
39 |
}, "Please specify a valid NIE number." );
|