From 432c33564c0159127087a8b55db039106ce549d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 19:44:36 +0200 Subject: [PATCH 01/13] Added NIF, NIE and CIF Spanish documents numbers validation * Added some translations to /localization * Added test suite --- src/additional/cifES.js | 50 ++++++++++++++++ src/additional/nieES.js | 44 ++++++++++++++ src/additional/nifES.js | 40 +++++++++++++ src/localization/messages_es.js | 5 +- src/localization/messages_es_AR.js | 5 +- src/localization/messages_fr.js | 5 +- src/localization/messages_it.js | 5 +- src/localization/messages_pt_BR.js | 5 +- src/localization/messages_pt_PT.js | 5 +- test/index.html | 1 + test/methods.js | 95 ++++++++++++++++++++++++++++++ 11 files changed, 254 insertions(+), 6 deletions(-) create mode 100644 src/additional/cifES.js create mode 100644 src/additional/nieES.js create mode 100644 src/additional/nifES.js diff --git a/src/additional/cifES.js b/src/additional/cifES.js new file mode 100644 index 000000000..08633a1fc --- /dev/null +++ b/src/additional/cifES.js @@ -0,0 +1,50 @@ +jQuery.validator.addMethod("cifES",function(value, element) { + + "use strict"; + + var n = 0, + suma = 0, + num = [], + CIF = value.toUpperCase(); + + + // Basic format test + if (!CIF.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] = CIF.substr(i, 1); + } + + // Algoritmo para comprobacion de codigos tipo CIF + suma = parseInt(num[2],10) + parseInt(num[4],10) + parseInt(num[6],10); + for (var temp1=0, temp2=0, count = 1; count < 8; count += 2){ + temp1 = 2 * parseInt(num[count],10); + temp1 += ''; + temp1 = temp1.substring(0,1); + temp2 = 2 * parseInt(num[count],10); + temp2 += ''; + temp2 = temp2.substring(1,2); + if (temp2 === '') { + temp2 = '0'; + } + + suma += (parseInt(temp1,10) + parseInt(temp2,10)); + } + suma += ''; + n = 10 - parseInt(suma.substring(suma.length-1, suma.length),10); + + if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(CIF)){ + CIF = n + ''; + if (parseInt(num[8],10) === parseInt(String.fromCharCode(64 + n),10) || parseInt(num[8],10) === parseInt(CIF.substring(CIF.length-1, CIF.length),10)){ + return true; + } else{ + return false; + } + } + + return false; + +}, "Por favor, escribe un CIF válido."); \ No newline at end of file diff --git a/src/additional/nieES.js b/src/additional/nieES.js new file mode 100644 index 000000000..2210d8126 --- /dev/null +++ b/src/additional/nieES.js @@ -0,0 +1,44 @@ +jQuery.validator.addMethod("nieES",function(value, element) { + + "use strict"; + + var n = 0, + suma = 0, + pos = '', + NIE = value.toUpperCase(), + cadenadni = "TRWAGMYFPDXBNJZSQVHLCKE"; + + // Basic format test + if (!NIE.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; + } + + + //comprobacion de NIEs + //T + if (/^[T]{1}/.test(NIE)){ + if (NIE[8] === /^[T]{1}[A-Z0-9]{8}$/.test(NIE)){ + return true; + }else{ + return false; + } + } + + //XYZ + if (/^[XYZ]{1}/.test(NIE)){ + pos = NIE.replace('X','0'); + pos = pos.replace('Y','1'); + pos = pos.replace('Z','2'); + pos = pos.substring(0, 8) % 23; + if (NIE[8] === cadenadni.substring(pos, pos + 1)){ + return true; + } + else{ + return false; + } + } + + return false; + +}, "Por favor, escribe un NIE válido."); + diff --git a/src/additional/nifES.js b/src/additional/nifES.js new file mode 100644 index 000000000..a606aa3a5 --- /dev/null +++ b/src/additional/nifES.js @@ -0,0 +1,40 @@ +jQuery.validator.addMethod("nifES",function(value, element) { + + "use strict"; + + var n = 0, + suma = 0, + num = [], + NIF = value.toUpperCase(), + letradni=NIF.charAt(8), + cadenadni = "TRWAGMYFPDXBNJZSQVHLCKE", + posicion = NIF.substring(8,0) % 23, + letra = cadenadni.charAt(posicion); + + // Basic format test + if (!NIF.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; + } + + // Comprobacion de NIFs + if (/^[0-9]{8}[A-Z]{1}$/.test(NIF)){ + if (letra === letradni){ + return true; + } + else{ + return false; + } + } + // Comprobacion de NIFs especiales + if (/^[KLM]{1}/.test(NIF)){ + if (NIF[8] === String.fromCharCode(64 + n)){ + return true; + } + else { + return false; + } + } + + return false; + +}, "Por favor, escribe un NIF válido"); 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 @@ + jQuery - Validation Test Suite diff --git a/test/methods.js b/test/methods.js index 55beadcc8..5711c8b25 100644 --- a/test/methods.js +++ b/test/methods.js @@ -1025,4 +1025,99 @@ test("zipcodeUS", function() { ok(!method( "123456-7890" ), "Invalid zip" ); }); +test("nifES", function() { + var method = methodTest("nifES"); + ok( method( "11441059P" ), "NIF valid" ); + ok( method( "80054306T" ), "NIF valid" ); + ok( method( "76048581R" ), "NIF valid" ); + ok( method( "28950849J" ), "NIF valid" ); + ok( method( "34048598L" ), "NIF valid" ); + ok( method( "28311529R" ), "NIF valid" ); + ok( method( "34673804Q" ), "NIF valid" ); + ok( method( "92133247P" ), "NIF valid" ); + ok( method( "77149717N" ), "NIF valid" ); + ok( method( "15762034L" ), "NIF valid" ); + ok( method( "05122654W" ), "NIF valid" ); + ok( method( "05122654w" ), "NIF valid: lower case" ); + ok(!method( "1144105R" ), "NIF invalid: less than 8 digits without zero" ); + ok(!method( "11441059 R" ), "NIF invalid: white space" ); + ok(!method( "11441059" ), "NIF invalid: no letter" ); + ok(!method( "11441059PR" ), "NIF invalid: two letters" ); + ok(!method( "11440059R" ), "NIF invalid: wrong number" ); + ok(!method( "11441059S" ), "NIF invalid: wrong letter" ); + ok(!method( "114410598R" ), "NIF invalid: > 8 digits" ); + ok(!method( "11441059-R" ), "NIF invalid: dash" ); + ok(!method( "asdasdasd" ), "NIF invalid: all letters" ); + ok(!method( "11.144.059R" ), "NIF invalid: two dots" ); + ok(!method( "05.122.654R" ), "NIF invalid: starts with 0 and dots" ); + ok(!method( "5.122.654-R" ), "NIF invalid: dots and dash" ); + ok(!method( "05.122.654-R" ), "NIF invalid: starts with zero and dot and dash" ); +}); + +test("nieES", function() { + var method = methodTest("nieES"); + ok( method( "X0093999K" ), "NIE valid" ); + ok( method( "X1923000Q" ), "NIE valid" ); + ok( method( "Z9669587R" ), "NIE valid" ); + ok( method( "Z8945005B" ), "NIE valid" ); + ok( method( "Z6663465W" ), "NIE valid" ); + ok( method( "Y7875935J" ), "NIE valid" ); + ok( method( "X3390130E" ), "NIE valid" ); + ok( method( "Y7699182S" ), "NIE valid" ); + ok( method( "Y1524243R" ), "NIE valid" ); + ok( method( "X3744072V" ), "NIE valid" ); + ok( method( "X7436800A" ), "NIE valid" ); + ok( method( "y7875935j" ), "NIE valid: lower case" ); + ok(!method( "X0093999 K" ), "NIE inválido: white space" ); + ok(!method( "X 0093999 K" ), "NIE inválido: white space" ); + ok(!method( "11441059" ), "NIE inválido: no letter" ); + ok(!method( "11441059PR" ), "NIE inválido: two letters" ); + ok(!method( "11440059R" ), "NIE inválido: wrong number" ); + ok(!method( "11441059S" ), "NIE inválido: wrong letter" ); + ok(!method( "114410598R" ), "NIE inválido: > 8 digits" ); + ok(!method( "11441059-R" ), "NIE inválido: dash" ); + ok(!method( "asdasdasd" ), "NIE inválido: all letters" ); + ok(!method( "11.144.059R" ), "NIE inválido: two dots" ); + ok(!method( "05.122.654R" ), "NIE inválido: starts with 0 and dots" ); + ok(!method( "5.122.654-R" ), "NIE inválido: dots and dash" ); + ok(!method( "05.122.654-R" ), "NIE inválido: starts with zero and dot and dash" ); +}); + +test("cifES", function() { + var method = methodTest("cifES"); + ok( method( "A79082244" ), "CIF valid" ); + ok( method( "A60917978" ), "CIF valid" ); + ok( method( "A39000013" ), "CIF valid" ); + ok( method( "B43522192" ), "CIF valid" ); + ok( method( "B38624334" ), "CIF valid" ); + ok( method( "G72102064" ), "CIF valid" ); + ok( method( "F41190612" ), "CIF valid" ); + ok( method( "J85081081" ), "CIF valid" ); + ok( method( "S98038813" ), "CIF valid" ); + ok( method( "G32937757" ), "CIF valid" ); + ok( method( "B46125746" ), "CIF valid" ); + ok( method( "C27827559" ), "CIF valid" ); + ok( method( "E48911572" ), "CIF valid" ); + ok( method( "s98038813" ), "CIF valid: lower case" ); + ok(!method( "K48911572" ), "CIF invalid: starts with K" ); + ok(!method( "L48911572" ), "CIF invalid: starts with L" ); + ok(!method( "M48911572" ), "CIF invalid: starts with M" ); + ok(!method( "X48911572" ), "CIF invalid: starts with X" ); + ok(!method( "Y48911572" ), "CIF invalid: starts with Y" ); + ok(!method( "Z48911572" ), "CIF invalid: starts with Z" ); + ok(!method( "M15661515" ), "CIF invalid" ); + ok(!method( "Z98038813" ), "CIF invalid: wrong letter" ); + ok(!method( "B 43522192" ), "CIF invalid: white spaces" ); + ok(!method( "43522192" ), "CIF invalid: missing letter" ); + ok(!method( "BB43522192" ), "CIF invalid: two letters" ); + ok(!method( "B53522192" ), "CIF invalid: wrong number" ); + ok(!method( "B433522192" ), "CIF invalid: > 8 digits" ); + ok(!method( "B3522192" ), "CIF invalid: < 8 digits" ); + ok(!method( "B-43522192" ), "CIF invalid: dash" ); + ok(!method( "Basdasdas" ), "CIF invalid: all letters" ); + ok(!method( "B43.522.192" ), "CIF invalid: dots" ); + ok(!method( "B-43.522.192" ), "CIF invalid: dots and dash" ); +}); + + })(jQuery); From afeca4fa54f508f7d3a7c86a87a6533ca536c2c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 20:23:27 +0200 Subject: [PATCH 02/13] NIF, NIE, CIF number validation: * Changed the default message to English --- src/additional/cifES.js | 2 +- src/additional/nieES.js | 2 +- src/additional/nifES.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/additional/cifES.js b/src/additional/cifES.js index 08633a1fc..e6964306a 100644 --- a/src/additional/cifES.js +++ b/src/additional/cifES.js @@ -47,4 +47,4 @@ jQuery.validator.addMethod("cifES",function(value, element) { return false; -}, "Por favor, escribe un CIF válido."); \ No newline at end of file +}, "Please specify a valid CIF number."); \ No newline at end of file diff --git a/src/additional/nieES.js b/src/additional/nieES.js index 2210d8126..ae6c84368 100644 --- a/src/additional/nieES.js +++ b/src/additional/nieES.js @@ -40,5 +40,5 @@ jQuery.validator.addMethod("nieES",function(value, element) { return false; -}, "Por favor, escribe un NIE válido."); +}, "Please specify a valid NIE number."); diff --git a/src/additional/nifES.js b/src/additional/nifES.js index a606aa3a5..e5eb70a01 100644 --- a/src/additional/nifES.js +++ b/src/additional/nifES.js @@ -37,4 +37,4 @@ jQuery.validator.addMethod("nifES",function(value, element) { return false; -}, "Por favor, escribe un NIF válido"); +}, "Please specify a valid NIF number."); From bcb9dfe1dcdf6a0194f617391954ba8de02a67b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 20:59:47 +0200 Subject: [PATCH 03/13] NIF, NIE, CIF numbers validation: * Fixed bug: Changed to string the strict comparison for num[8]. * Fixed: Some comments translated to english. --- src/additional/cifES.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/additional/cifES.js b/src/additional/cifES.js index e6964306a..d547d0a86 100644 --- a/src/additional/cifES.js +++ b/src/additional/cifES.js @@ -18,7 +18,7 @@ jQuery.validator.addMethod("cifES",function(value, element) { num[i] = CIF.substr(i, 1); } - // Algoritmo para comprobacion de codigos tipo CIF + // Algorithm for checking CIF codes suma = parseInt(num[2],10) + parseInt(num[4],10) + parseInt(num[6],10); for (var temp1=0, temp2=0, count = 1; count < 8; count += 2){ temp1 = 2 * parseInt(num[count],10); @@ -38,7 +38,7 @@ jQuery.validator.addMethod("cifES",function(value, element) { if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(CIF)){ CIF = n + ''; - if (parseInt(num[8],10) === parseInt(String.fromCharCode(64 + n),10) || parseInt(num[8],10) === parseInt(CIF.substring(CIF.length-1, CIF.length),10)){ + if (num[8] === String.fromCharCode(64 + n) || num[8] === CIF.substring(CIF.length-1, CIF.length)){ return true; } else{ return false; From 57c5696760297e805f3d1c40035017ff8a35ec7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 21:10:26 +0200 Subject: [PATCH 04/13] NIF, NIE, CIF numbers validation: * Optimized return in cifES.js --- src/additional/cifES.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/additional/cifES.js b/src/additional/cifES.js index d547d0a86..62f2f4cf3 100644 --- a/src/additional/cifES.js +++ b/src/additional/cifES.js @@ -38,11 +38,7 @@ jQuery.validator.addMethod("cifES",function(value, element) { if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(CIF)){ CIF = n + ''; - if (num[8] === String.fromCharCode(64 + n) || num[8] === CIF.substring(CIF.length-1, CIF.length)){ - return true; - } else{ - return false; - } + return (num[8] === String.fromCharCode(64 + n) || num[8] === CIF.substring(CIF.length-1, CIF.length)); } return false; From 98f9b2b7341300bf81a3e25a38ca1d6ceea215cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 21:13:48 +0200 Subject: [PATCH 05/13] NIF, NIE and CIF numbers validation: * Optimized returns in nifES.js --- src/additional/nifES.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/additional/nifES.js b/src/additional/nifES.js index e5eb70a01..e6b2942aa 100644 --- a/src/additional/nifES.js +++ b/src/additional/nifES.js @@ -18,21 +18,11 @@ jQuery.validator.addMethod("nifES",function(value, element) { // Comprobacion de NIFs if (/^[0-9]{8}[A-Z]{1}$/.test(NIF)){ - if (letra === letradni){ - return true; - } - else{ - return false; - } + return (letra === letradni); } // Comprobacion de NIFs especiales if (/^[KLM]{1}/.test(NIF)){ - if (NIF[8] === String.fromCharCode(64 + n)){ - return true; - } - else { - return false; - } + return (NIF[8] === String.fromCharCode(64 + n)); } return false; From 34ba407f9adf9aa83ce3edfae9fe5863844ec743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 21:16:21 +0200 Subject: [PATCH 06/13] NIF, NIE and CIF numbers validation: * Optimized returns in nieES.js --- src/additional/nieES.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/additional/nieES.js b/src/additional/nieES.js index ae6c84368..4aaf835de 100644 --- a/src/additional/nieES.js +++ b/src/additional/nieES.js @@ -17,11 +17,7 @@ jQuery.validator.addMethod("nieES",function(value, element) { //comprobacion de NIEs //T if (/^[T]{1}/.test(NIE)){ - if (NIE[8] === /^[T]{1}[A-Z0-9]{8}$/.test(NIE)){ - return true; - }else{ - return false; - } + return (NIE[8] === /^[T]{1}[A-Z0-9]{8}$/.test(NIE)); } //XYZ @@ -30,12 +26,7 @@ jQuery.validator.addMethod("nieES",function(value, element) { pos = pos.replace('Y','1'); pos = pos.replace('Z','2'); pos = pos.substring(0, 8) % 23; - if (NIE[8] === cadenadni.substring(pos, pos + 1)){ - return true; - } - else{ - return false; - } + return (NIE[8] === cadenadni.substring(pos, pos + 1)); } return false; From e09c41a897b693cb48a5ebd101e965a518a4451c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 22:34:32 +0200 Subject: [PATCH 07/13] NIF, NIE and CIF numbers validation: * cifES.js refactorized by 'nschonni' --- src/additional/cifES.js | 58 +++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/additional/cifES.js b/src/additional/cifES.js index 62f2f4cf3..44929d7a6 100644 --- a/src/additional/cifES.js +++ b/src/additional/cifES.js @@ -1,46 +1,36 @@ -jQuery.validator.addMethod("cifES",function(value, element) { - +jQuery.validator.addMethod( "cifES", function ( value, element ) { "use strict"; - - var n = 0, - suma = 0, + + var n, + sum, num = [], CIF = value.toUpperCase(); - - + // Basic format test - if (!CIF.match('((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)')) { + if ( !CIF.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] = CIF.substr(i, 1); + for ( var i = 0; i < 9; i++ ) { + num[i] = parseInt( CIF.charAt( i ), 10 ); } - + // Algorithm for checking CIF codes - suma = parseInt(num[2],10) + parseInt(num[4],10) + parseInt(num[6],10); - for (var temp1=0, temp2=0, count = 1; count < 8; count += 2){ - temp1 = 2 * parseInt(num[count],10); - temp1 += ''; - temp1 = temp1.substring(0,1); - temp2 = 2 * parseInt(num[count],10); - temp2 += ''; - temp2 = temp2.substring(1,2); - if (temp2 === '') { - temp2 = '0'; - } - - suma += (parseInt(temp1,10) + parseInt(temp2,10)); + 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 ) ); } - suma += ''; - n = 10 - parseInt(suma.substring(suma.length-1, suma.length),10); - - if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(CIF)){ - CIF = n + ''; - return (num[8] === String.fromCharCode(64 + n) || num[8] === CIF.substring(CIF.length-1, CIF.length)); + + if ( /^[ABCDEFGHJNPQRSUVW]{1}/.test( CIF ) ) { + sum += ''; + n = 10 - parseInt( sum.charAt( sum.length - 1 ), 10 ); + CIF += n; + return ( num[ 8 ].toString() === String.fromCharCode( 64 + n ) || num[ 8 ].toString() === CIF.charAt( CIF.length - 1 ) ); } - + return false; - -}, "Please specify a valid CIF number."); \ No newline at end of file + +}, "Please specify a valid CIF number." ); \ No newline at end of file From e91d70465e5b506a57ecc51ed02d9fabc1d1a31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 23:01:18 +0200 Subject: [PATCH 08/13] NIF, NIE and CIF numbers validation: * Removed unused variables. * Refactorized. --- src/additional/nieES.js | 13 ++++++------- src/additional/nifES.js | 7 ++----- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/additional/nieES.js b/src/additional/nieES.js index 4aaf835de..8ab084507 100644 --- a/src/additional/nieES.js +++ b/src/additional/nieES.js @@ -2,9 +2,7 @@ jQuery.validator.addMethod("nieES",function(value, element) { "use strict"; - var n = 0, - suma = 0, - pos = '', + var pos = '', NIE = value.toUpperCase(), cadenadni = "TRWAGMYFPDXBNJZSQVHLCKE"; @@ -22,10 +20,11 @@ jQuery.validator.addMethod("nieES",function(value, element) { //XYZ if (/^[XYZ]{1}/.test(NIE)){ - pos = NIE.replace('X','0'); - pos = pos.replace('Y','1'); - pos = pos.replace('Z','2'); - pos = pos.substring(0, 8) % 23; + pos = NIE.replace('X','0') + .replace('Y','1') + .replace('Z','2') + .substring(0, 8) % 23; + return (NIE[8] === cadenadni.substring(pos, pos + 1)); } diff --git a/src/additional/nifES.js b/src/additional/nifES.js index e6b2942aa..66bc7a55e 100644 --- a/src/additional/nifES.js +++ b/src/additional/nifES.js @@ -2,10 +2,7 @@ jQuery.validator.addMethod("nifES",function(value, element) { "use strict"; - var n = 0, - suma = 0, - num = [], - NIF = value.toUpperCase(), + var NIF = value.toUpperCase(), letradni=NIF.charAt(8), cadenadni = "TRWAGMYFPDXBNJZSQVHLCKE", posicion = NIF.substring(8,0) % 23, @@ -22,7 +19,7 @@ jQuery.validator.addMethod("nifES",function(value, element) { } // Comprobacion de NIFs especiales if (/^[KLM]{1}/.test(NIF)){ - return (NIF[8] === String.fromCharCode(64 + n)); + return (NIF[8] === String.fromCharCode(64)); } return false; From bbbbdb82be8a02536a42f615ed02ec1cc580e56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 23:04:12 +0200 Subject: [PATCH 09/13] NIF, NIE and CIF numbers validation: * As suggested by 'nschonni', cadenadni.substring(pos, pos + 1) replaced with charAt(pos) --- src/additional/nieES.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/additional/nieES.js b/src/additional/nieES.js index 8ab084507..4c9bd01cf 100644 --- a/src/additional/nieES.js +++ b/src/additional/nieES.js @@ -25,7 +25,7 @@ jQuery.validator.addMethod("nieES",function(value, element) { .replace('Z','2') .substring(0, 8) % 23; - return (NIE[8] === cadenadni.substring(pos, pos + 1)); + return (NIE[8] === cadenadni.charAt(pos)); } return false; From 144210143154a192552b65cd830631aed7f7f586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 23:12:42 +0200 Subject: [PATCH 10/13] NIF, NIE and CIF numbers validation: * Translated some variables to English. * Applied jQuery style guide. --- src/additional/nieES.js | 23 +++++++++++------------ src/additional/nifES.js | 25 ++++++++++++------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/additional/nieES.js b/src/additional/nieES.js index 4c9bd01cf..bd0032070 100644 --- a/src/additional/nieES.js +++ b/src/additional/nieES.js @@ -1,31 +1,30 @@ -jQuery.validator.addMethod("nieES",function(value, element) { - +jQuery.validator.addMethod( "nieES", function ( value, element ) { "use strict"; var pos = '', NIE = value.toUpperCase(), - cadenadni = "TRWAGMYFPDXBNJZSQVHLCKE"; + stringDNI = "TRWAGMYFPDXBNJZSQVHLCKE"; // Basic format test - if (!NIE.match('((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)')) { + if ( !NIE.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; } //comprobacion de NIEs //T - if (/^[T]{1}/.test(NIE)){ - return (NIE[8] === /^[T]{1}[A-Z0-9]{8}$/.test(NIE)); + if ( /^[T]{1}/.test( NIE ) ){ + return ( NIE[ 8 ] === /^[T]{1}[A-Z0-9]{8}$/.test( NIE ) ); } //XYZ - if (/^[XYZ]{1}/.test(NIE)){ - pos = NIE.replace('X','0') - .replace('Y','1') - .replace('Z','2') - .substring(0, 8) % 23; + if ( /^[XYZ]{1}/.test( NIE ) ){ + pos = NIE.replace( 'X','0' ) + .replace( 'Y','1' ) + .replace( 'Z','2' ) + .substring( 0, 8 ) % 23; - return (NIE[8] === cadenadni.charAt(pos)); + return ( NIE[ 8 ] === stringDNI.charAt( pos ) ); } return false; diff --git a/src/additional/nifES.js b/src/additional/nifES.js index 66bc7a55e..e42edf6d4 100644 --- a/src/additional/nifES.js +++ b/src/additional/nifES.js @@ -1,25 +1,24 @@ -jQuery.validator.addMethod("nifES",function(value, element) { - +jQuery.validator.addMethod( "nifES", function ( value, element ) { "use strict"; var NIF = value.toUpperCase(), - letradni=NIF.charAt(8), - cadenadni = "TRWAGMYFPDXBNJZSQVHLCKE", - posicion = NIF.substring(8,0) % 23, - letra = cadenadni.charAt(posicion); + letterDNI=NIF.charAt( 8 ), + stringDNI = "TRWAGMYFPDXBNJZSQVHLCKE", + position = NIF.substring( 8,0 ) % 23, + letter = stringDNI.charAt( position ); // Basic format test - if (!NIF.match('((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)')) { + if ( !NIF.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; } - // Comprobacion de NIFs - if (/^[0-9]{8}[A-Z]{1}$/.test(NIF)){ - return (letra === letradni); + // Test NIF + if ( /^[0-9]{8}[A-Z]{1}$/.test( NIF ) ){ + return ( letter === letterDNI ); } - // Comprobacion de NIFs especiales - if (/^[KLM]{1}/.test(NIF)){ - return (NIF[8] === String.fromCharCode(64)); + // Test specials NIF (starts with K, L or M) + if ( /^[KLM]{1}/.test( NIF ) ){ + return ( NIF[ 8 ] === String.fromCharCode( 64 ) ); } return false; From e8b39bc08d51a567168b7e8e7deb59c25138e462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Mon, 29 Jul 2013 23:55:11 +0200 Subject: [PATCH 11/13] NIF, NIE and CIF validation numbers: * 'n' variable changed to 'controlDigit'. * Fixed some code styling: spaces after comas, and spaces before curly braces. * Removed NIF, NIE and CIF variables with value = value.toUpperCase(). * Inlined just one use variables (cadenadni, and some others). * Removed unused variables. * Some substring() replaced with charAt(). * Comments translated to English. --- src/additional/cifES.js | 20 +++++++++++--------- src/additional/nieES.js | 30 ++++++++++++++---------------- src/additional/nifES.js | 18 +++++++----------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/additional/cifES.js b/src/additional/cifES.js index 44929d7a6..ab173e849 100644 --- a/src/additional/cifES.js +++ b/src/additional/cifES.js @@ -1,18 +1,19 @@ jQuery.validator.addMethod( "cifES", function ( value, element ) { "use strict"; - var n, - sum, + var sum, num = [], - CIF = value.toUpperCase(); + controlDigit; + + value = value.toUpperCase(); // Basic format test - if ( !CIF.match( '((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)' ) ) { + 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( CIF.charAt( i ), 10 ); + num[ i ] = parseInt( value.charAt( i ), 10 ); } // Algorithm for checking CIF codes @@ -24,11 +25,12 @@ jQuery.validator.addMethod( "cifES", function ( value, element ) { sum += parseInt( tmp.charAt( 0 ), 10 ) + ( secondDigit === '' ? 0 : parseInt( secondDigit, 10 ) ); } - if ( /^[ABCDEFGHJNPQRSUVW]{1}/.test( CIF ) ) { + // CIF test + if ( /^[ABCDEFGHJNPQRSUVW]{1}/.test( value ) ) { sum += ''; - n = 10 - parseInt( sum.charAt( sum.length - 1 ), 10 ); - CIF += n; - return ( num[ 8 ].toString() === String.fromCharCode( 64 + n ) || num[ 8 ].toString() === CIF.charAt( CIF.length - 1 ) ); + 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; diff --git a/src/additional/nieES.js b/src/additional/nieES.js index bd0032070..a99ace6fa 100644 --- a/src/additional/nieES.js +++ b/src/additional/nieES.js @@ -1,33 +1,31 @@ jQuery.validator.addMethod( "nieES", function ( value, element ) { "use strict"; - var pos = '', - NIE = value.toUpperCase(), - stringDNI = "TRWAGMYFPDXBNJZSQVHLCKE"; + var position = value.toUpperCase() + .replace( 'X', '0' ) + .replace( 'Y', '1' ) + .replace( 'Z', '2' ) + .substring( 0, 8 ) % 23; + + value = value.toUpperCase(); // Basic format test - if ( !NIE.match('((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)') ) { + 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; } - - //comprobacion de NIEs + // Test NIE //T - if ( /^[T]{1}/.test( NIE ) ){ - return ( NIE[ 8 ] === /^[T]{1}[A-Z0-9]{8}$/.test( NIE ) ); + if ( /^[T]{1}/.test( value ) ) { + return ( value[ 8 ] === /^[T]{1}[A-Z0-9]{8}$/.test( value ) ); } //XYZ - if ( /^[XYZ]{1}/.test( NIE ) ){ - pos = NIE.replace( 'X','0' ) - .replace( 'Y','1' ) - .replace( 'Z','2' ) - .substring( 0, 8 ) % 23; - - return ( NIE[ 8 ] === stringDNI.charAt( pos ) ); + if ( /^[XYZ]{1}/.test( value ) ) { + return ( value[ 8 ] === "TRWAGMYFPDXBNJZSQVHLCKE".charAt( position ) ); } return false; -}, "Please specify a valid NIE number."); +}, "Please specify a valid NIE number." ); diff --git a/src/additional/nifES.js b/src/additional/nifES.js index e42edf6d4..74a19e10d 100644 --- a/src/additional/nifES.js +++ b/src/additional/nifES.js @@ -1,26 +1,22 @@ jQuery.validator.addMethod( "nifES", function ( value, element ) { "use strict"; - var NIF = value.toUpperCase(), - letterDNI=NIF.charAt( 8 ), - stringDNI = "TRWAGMYFPDXBNJZSQVHLCKE", - position = NIF.substring( 8,0 ) % 23, - letter = stringDNI.charAt( position ); + value = value.toUpperCase(); // Basic format test - if ( !NIF.match('((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)') ) { + 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( NIF ) ){ - return ( letter === letterDNI ); + 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( NIF ) ){ - return ( NIF[ 8 ] === String.fromCharCode( 64 ) ); + if ( /^[KLM]{1}/.test( value ) ) { + return ( value[ 8 ] === String.fromCharCode( 64 ) ); } return false; -}, "Please specify a valid NIF number."); +}, "Please specify a valid NIF number." ); From 0b03bbff01b760a1e980f354252fb0700a0ae473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Tue, 30 Jul 2013 00:10:24 +0200 Subject: [PATCH 12/13] NIF, NIE and CIF numbers validation: * Removed 'position' variable: inlined to last test() --- src/additional/nieES.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/additional/nieES.js b/src/additional/nieES.js index a99ace6fa..169f8de31 100644 --- a/src/additional/nieES.js +++ b/src/additional/nieES.js @@ -1,12 +1,6 @@ jQuery.validator.addMethod( "nieES", function ( value, element ) { "use strict"; - var position = value.toUpperCase() - .replace( 'X', '0' ) - .replace( 'Y', '1' ) - .replace( 'Z', '2' ) - .substring( 0, 8 ) % 23; - value = value.toUpperCase(); // Basic format test @@ -22,7 +16,14 @@ jQuery.validator.addMethod( "nieES", function ( value, element ) { //XYZ if ( /^[XYZ]{1}/.test( value ) ) { - return ( value[ 8 ] === "TRWAGMYFPDXBNJZSQVHLCKE".charAt( position ) ); + return ( + value[ 8 ] === "TRWAGMYFPDXBNJZSQVHLCKE".charAt( + value.replace( 'X', '0' ) + .replace( 'Y', '1' ) + .replace( 'Z', '2' ) + .substring( 0, 8 ) % 23 + ) + ); } return false; From e49184961eac61b9df8e49b0eae7d3bdea84ae59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Mart=C3=ADn?= Date: Tue, 30 Jul 2013 00:14:11 +0200 Subject: [PATCH 13/13] NIF, NIE and CIF numbers validation: * Fixed indentation at nieES.js --- src/additional/nieES.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/additional/nieES.js b/src/additional/nieES.js index 169f8de31..7e4761c88 100644 --- a/src/additional/nieES.js +++ b/src/additional/nieES.js @@ -19,9 +19,9 @@ jQuery.validator.addMethod( "nieES", function ( value, element ) { return ( value[ 8 ] === "TRWAGMYFPDXBNJZSQVHLCKE".charAt( value.replace( 'X', '0' ) - .replace( 'Y', '1' ) - .replace( 'Z', '2' ) - .substring( 0, 8 ) % 23 + .replace( 'Y', '1' ) + .replace( 'Z', '2' ) + .substring( 0, 8 ) % 23 ) ); }