Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional: Refine cifES and nieES algorithms (#1826)
- Loading branch information
1 parent
5c0c8ff
commit e7422d6
Showing
3 changed files
with
234 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,35 @@ | ||
/* | ||
* The número de identidad de extranjero ( NIE )is a code used to identify the non-nationals in Spain | ||
* The NIE (Número de Identificación de Extranjero) is a Spanish tax identification number assigned by the Spanish | ||
* authorities to any foreigner. | ||
* | ||
* The NIE is the equivalent of a Spaniards Número de Identificación Fiscal (NIF) which serves as a fiscal | ||
* identification number. The CIF number (Certificado de Identificación Fiscal) is equivalent to the NIF, but applies to | ||
* companies rather than individuals. The NIE consists of an 'X' or 'Y' followed by 7 or 8 digits then another letter. | ||
*/ | ||
$.validator.addMethod( "nieES", function( value ) { | ||
"use strict"; | ||
|
||
value = value.toUpperCase(); | ||
var nieRegEx = new RegExp( /^[MXYZ]{1}[0-9]{7,8}[TRWAGMYFPDXBNJZSQVHLCKET]{1}$/gi ); | ||
var validChars = "TRWAGMYFPDXBNJZSQVHLCKET", | ||
letter = value.substr( value.length - 1 ).toUpperCase(), | ||
number; | ||
|
||
// Basic format test | ||
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}$)" ) ) { | ||
value = value.toString().toUpperCase(); | ||
|
||
// Quick format test | ||
if ( value.length > 10 || value.length < 9 || !nieRegEx.test( value ) ) { | ||
return false; | ||
} | ||
|
||
// Test NIE | ||
//T | ||
if ( /^[T]{1}/.test( value ) ) { | ||
return ( value[ 8 ] === /^[T]{1}[A-Z0-9]{8}$/.test( value ) ); | ||
} | ||
// X means same number | ||
// Y means number + 10000000 | ||
// Z means number + 20000000 | ||
value = value.replace( /^[X]/, "0" ) | ||
.replace( /^[Y]/, "1" ) | ||
.replace( /^[Z]/, "2" ); | ||
|
||
//XYZ | ||
if ( /^[XYZ]{1}/.test( value ) ) { | ||
return ( | ||
value[ 8 ] === "TRWAGMYFPDXBNJZSQVHLCKE".charAt( | ||
value.replace( "X", "0" ) | ||
.replace( "Y", "1" ) | ||
.replace( "Z", "2" ) | ||
.substring( 0, 8 ) % 23 | ||
) | ||
); | ||
} | ||
number = value.length === 9 ? value.substr( 0, 8 ) : value.substr( 0, 9 ); | ||
|
||
return false; | ||
return validChars.charAt( parseInt( number, 10 ) % 23 ) === letter; | ||
|
||
}, "Please specify a valid NIE number." ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters