diff --git a/src/additional/cifES.js b/src/additional/cifES.js new file mode 100644 index 000000000..ab173e849 --- /dev/null +++ b/src/additional/cifES.js @@ -0,0 +1,38 @@ +jQuery.validator.addMethod( "cifES", function ( value, element ) { + "use strict"; + + var sum, + num = [], + controlDigit; + + value = value.toUpperCase(); + + // 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}$)' ) ) { + return false; + } + + for ( var i = 0; i < 9; i++ ) { + num[ i ] = parseInt( value.charAt( i ), 10 ); + } + + // Algorithm for checking CIF codes + sum = num[ 2 ] + num[ 4 ] + num[ 6 ]; + for ( var count = 1; count < 8; count += 2 ) { + var tmp = ( 2 * num[ count ] ).toString(), + secondDigit = tmp.charAt( 1 ); + + sum += parseInt( tmp.charAt( 0 ), 10 ) + ( secondDigit === '' ? 0 : parseInt( secondDigit, 10 ) ); + } + + // CIF test + if ( /^[ABCDEFGHJNPQRSUVW]{1}/.test( value ) ) { + sum += ''; + controlDigit = 10 - parseInt( sum.charAt( sum.length - 1 ), 10 ); + value += controlDigit; + return ( num[ 8 ].toString() === String.fromCharCode( 64 + controlDigit ) || num[ 8 ].toString() === value.charAt( value.length - 1 ) ); + } + + return false; + +}, "Please specify a valid CIF number." ); \ No newline at end of file diff --git a/src/additional/nieES.js b/src/additional/nieES.js new file mode 100644 index 000000000..7e4761c88 --- /dev/null +++ b/src/additional/nieES.js @@ -0,0 +1,32 @@ +jQuery.validator.addMethod( "nieES", function ( value, element ) { + "use strict"; + + value = value.toUpperCase(); + + // 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}$)') ) { + return false; + } + + // Test NIE + //T + if ( /^[T]{1}/.test( value ) ) { + return ( value[ 8 ] === /^[T]{1}[A-Z0-9]{8}$/.test( value ) ); + } + + //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 + ) + ); + } + + return false; + +}, "Please specify a valid NIE number." ); + diff --git a/src/additional/nifES.js b/src/additional/nifES.js new file mode 100644 index 000000000..74a19e10d --- /dev/null +++ b/src/additional/nifES.js @@ -0,0 +1,22 @@ +jQuery.validator.addMethod( "nifES", function ( value, element ) { + "use strict"; + + value = value.toUpperCase(); + + // 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}$)') ) { + return false; + } + + // Test NIF + if ( /^[0-9]{8}[A-Z]{1}$/.test( value ) ) { + return ( "TRWAGMYFPDXBNJZSQVHLCKE".charAt( value.substring( 8, 0 ) % 23 ) === value.charAt( 8 ) ); + } + // Test specials NIF (starts with K, L or M) + if ( /^[KLM]{1}/.test( value ) ) { + return ( value[ 8 ] === String.fromCharCode( 64 ) ); + } + + return false; + +}, "Please specify a valid NIF number." ); diff --git a/src/localization/messages_es.js b/src/localization/messages_es.js index d0f48aa77..34f86a1f8 100644 --- a/src/localization/messages_es.js +++ b/src/localization/messages_es.js @@ -20,6 +20,9 @@ rangelength: $.validator.format("Por favor, escribe un valor entre {0} y {1} caracteres."), range: $.validator.format("Por favor, escribe un valor entre {0} y {1}."), max: $.validator.format("Por favor, escribe un valor menor o igual a {0}."), - min: $.validator.format("Por favor, escribe un valor mayor o igual a {0}.") + min: $.validator.format("Por favor, escribe un valor mayor o igual a {0}."), + nifES: "Por favor, escribe un NIF válido.", + nieES: "Por favor, escribe un NIE válido.", + cifES: "Por favor, escribe un CIF válido." }); }(jQuery)); \ No newline at end of file diff --git a/src/localization/messages_es_AR.js b/src/localization/messages_es_AR.js index a93b33736..1741ca2ae 100644 --- a/src/localization/messages_es_AR.js +++ b/src/localization/messages_es_AR.js @@ -21,6 +21,9 @@ rangelength: $.validator.format("Por favor, escribí un valor entre {0} y {1} caracteres."), range: $.validator.format("Por favor, escribí un valor entre {0} y {1}."), max: $.validator.format("Por favor, escribí un valor menor o igual a {0}."), - min: $.validator.format("Por favor, escribí un valor mayor o igual a {0}.") + min: $.validator.format("Por favor, escribí un valor mayor o igual a {0}."), + nifES: "Por favor, escribí un NIF válido.", + nieES: "Por favor, escribí un NIE válido.", + cifES: "Por favor, escribí un CIF válido." }); }(jQuery)); diff --git a/src/localization/messages_fr.js b/src/localization/messages_fr.js index 6e0c1fe17..b5114b007 100644 --- a/src/localization/messages_fr.js +++ b/src/localization/messages_fr.js @@ -42,6 +42,9 @@ creditcardtypes: "Veuillez fournir un numéro de carte de crédit valide.", ipv4: "Veuillez fournir une adresse IP v4 valide.", ipv6: "Veuillez fournir une adresse IP v6 valide.", - require_from_group: "Veuillez fournir au moins {0} de ces champs." + require_from_group: "Veuillez fournir au moins {0} de ces champs.", + nifES: "Veuillez fournir un numéro NIF valide.", + nieES: "Veuillez fournir un numéro NIE valide.", + cifES: "Veuillez fournir un numéro CIF valide." }); }(jQuery)); \ No newline at end of file diff --git a/src/localization/messages_it.js b/src/localization/messages_it.js index 63188fc65..52a3d822a 100644 --- a/src/localization/messages_it.js +++ b/src/localization/messages_it.js @@ -20,6 +20,9 @@ rangelength: $.validator.format("Inserisci un valore compreso tra {0} e {1} caratteri."), range: $.validator.format("Inserisci un valore compreso tra {0} e {1}."), max: $.validator.format("Inserisci un valore minore o uguale a {0}."), - min: $.validator.format("Inserisci un valore maggiore o uguale a {0}.") + min: $.validator.format("Inserisci un valore maggiore o uguale a {0}."), + nifES: "Inserisci un NIF valido.", + nieES: "Inserisci un NIE valido.", + cifES: "Inserisci un CIF valido." }); }(jQuery)); \ No newline at end of file diff --git a/src/localization/messages_pt_BR.js b/src/localization/messages_pt_BR.js index b9728f2e2..e2594db73 100644 --- a/src/localization/messages_pt_BR.js +++ b/src/localization/messages_pt_BR.js @@ -21,6 +21,9 @@ rangelength: $.validator.format("Por favor, forneça um valor entre {0} e {1} caracteres de comprimento."), range: $.validator.format("Por favor, forneça um valor entre {0} e {1}."), max: $.validator.format("Por favor, forneça um valor menor ou igual a {0}."), - min: $.validator.format("Por favor, forneça um valor maior ou igual a {0}.") + min: $.validator.format("Por favor, forneça um valor maior ou igual a {0}."), + nifES: "Por favor, forneça um NIF válido.", + nieES: "Por favor, forneça um NIE válido.", + cifEE: "Por favor, forneça um CIF válido." }); }(jQuery)); diff --git a/src/localization/messages_pt_PT.js b/src/localization/messages_pt_PT.js index 73fa98bdb..390f9d4a8 100644 --- a/src/localization/messages_pt_PT.js +++ b/src/localization/messages_pt_PT.js @@ -21,6 +21,9 @@ rangelength: $.validator.format("Por favor, introduza entre {0} e {1} caracteres."), range: $.validator.format("Por favor, introduza um valor entre {0} e {1}."), max: $.validator.format("Por favor, introduza um valor menor ou igual a {0}."), - min: $.validator.format("Por favor, introduza um valor maior ou igual a {0}.") + min: $.validator.format("Por favor, introduza um valor maior ou igual a {0}."), + nifES: "Por favor, introduza um NIF válido.", + nieES: "Por favor, introduza um NIE válido.", + cifES: "Por favor, introduza um CIF válido." }); }(jQuery)); diff --git a/test/index.html b/test/index.html index 308b4ab84..0e18a5331 100644 --- a/test/index.html +++ b/test/index.html @@ -1,6 +1,7 @@
+