From 615fe493fe8c596a8a9eb427dd3521e333b6b0d5 Mon Sep 17 00:00:00 2001 From: Robbert Wethmar Date: Wed, 6 Feb 2013 12:46:36 +0100 Subject: [PATCH 1/6] add dutch phone number validation --- additional-methods.js | 9 ++++++++- localization/messages_nl.js | 6 +++++- test/methods.js | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/additional-methods.js b/additional-methods.js index a133df787..f709cfbf5 100644 --- a/additional-methods.js +++ b/additional-methods.js @@ -155,7 +155,14 @@ jQuery.validator.addMethod("dateITA", function(value, element) { jQuery.validator.addMethod("dateNL", function(value, element) { return this.optional(element) || /^(0?[1-9]|[12]\d|3[01])[\.\/\-](0?[1-9]|1[012])[\.\/\-]([12]\d)?(\d\d)$/.test(value); -}, "Vul hier een geldige datum in."); +}, "Please enter a correct date"); + +/** + * Dutch phone numbers have 10 digits, or 11 and start with +31. + */ +jQuery.validator.addMethod("phoneNL", function(value, element) { + return this.optional(element) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9]){8}$/.test(value); +}, "Please specify a valid phone number."); jQuery.validator.addMethod("time", function(value, element) { return this.optional(element) || /^([01]\d|2[0-3])(:[0-5]\d){1,2}$/.test(value); diff --git a/localization/messages_nl.js b/localization/messages_nl.js index dd16be28f..1e2d1d5eb 100644 --- a/localization/messages_nl.js +++ b/localization/messages_nl.js @@ -20,6 +20,10 @@ rangelength: $.validator.format("Vul hier een waarde in van minimaal {0} en maximaal {1} tekens."), range: $.validator.format("Vul hier een waarde in van minimaal {0} en maximaal {1}."), max: $.validator.format("Vul hier een waarde in kleiner dan of gelijk aan {0}."), - min: $.validator.format("Vul hier een waarde in groter dan of gelijk aan {0}.") + min: $.validator.format("Vul hier een waarde in groter dan of gelijk aan {0}."), + + // for validations in additional-methods.js + dateNL: "Vul hier een geldige datum in.", + phoneNL: "Vul hier een geldig Nederlands telefoonnummer in." }); }(jQuery)); \ No newline at end of file diff --git a/test/methods.js b/test/methods.js index 36ddde727..daaafc030 100644 --- a/test/methods.js +++ b/test/methods.js @@ -590,6 +590,21 @@ test("dateNL", function() { ok(!method( "01.01.190" ), "Invalid date NL" ); }); +test("phoneNL", function() { + var method = methodTest("phoneNL"); + ok( method( "0701234567"), "Valid phone NL"); + ok( method( "0687654321"), "Valid phone NL"); + ok( method( "020-1234567"), "Valid phone NL"); + ok( method( "020 - 12 34 567"), "Valid phone NL"); + ok( method( "010-2345678"), "Valid phone NL"); + ok( method( "+3120-1234567"), "Valid phone NL"); + ok( method( "+31(0)10-2345678"), "Valid phone NL"); + ok(!method( "020-123456"), "Invalid phone NL"); // too short + ok(!method( "020-12345678"), "Invalid phone NL"); // too long + ok(!method( "-0201234567"), "Invalid phone NL"); + ok(!method( "+310201234567"), "Invalid phone NL"); // no 0 after +31 allowed +}); + test("time", function() { var method = methodTest("time"); ok( method("00:00"), "Valid time, lower bound" ); From 119bb3504802c7c8cbf4cf265e6dd86265bf8621 Mon Sep 17 00:00:00 2001 From: Robbert Wethmar Date: Wed, 6 Feb 2013 13:08:40 +0100 Subject: [PATCH 2/6] add dutch mobile phone number validation --- additional-methods.js | 5 +++++ localization/messages_nl.js | 3 ++- test/methods.js | 23 ++++++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/additional-methods.js b/additional-methods.js index f709cfbf5..e8963e853 100644 --- a/additional-methods.js +++ b/additional-methods.js @@ -164,6 +164,11 @@ jQuery.validator.addMethod("phoneNL", function(value, element) { return this.optional(element) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9]){8}$/.test(value); }, "Please specify a valid phone number."); +jQuery.validator.addMethod("mobileNL", function(value, element) { + return this.optional(element) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)6((\s|\s?\-\s?)?[0-9]){8}$/.test(value); +}, "Please specify a valid mobile number"); + + jQuery.validator.addMethod("time", function(value, element) { return this.optional(element) || /^([01]\d|2[0-3])(:[0-5]\d){1,2}$/.test(value); }, "Please enter a valid time, between 00:00 and 23:59"); diff --git a/localization/messages_nl.js b/localization/messages_nl.js index 1e2d1d5eb..0dbaad1c2 100644 --- a/localization/messages_nl.js +++ b/localization/messages_nl.js @@ -24,6 +24,7 @@ // for validations in additional-methods.js dateNL: "Vul hier een geldige datum in.", - phoneNL: "Vul hier een geldig Nederlands telefoonnummer in." + phoneNL: "Vul hier een geldig Nederlands telefoonnummer in.", + phoneNL: "Vul hier een geldig Nederlands mobiel telefoonnummer in." }); }(jQuery)); \ No newline at end of file diff --git a/test/methods.js b/test/methods.js index daaafc030..438771c57 100644 --- a/test/methods.js +++ b/test/methods.js @@ -599,12 +599,29 @@ test("phoneNL", function() { ok( method( "010-2345678"), "Valid phone NL"); ok( method( "+3120-1234567"), "Valid phone NL"); ok( method( "+31(0)10-2345678"), "Valid phone NL"); - ok(!method( "020-123456"), "Invalid phone NL"); // too short - ok(!method( "020-12345678"), "Invalid phone NL"); // too long + ok(!method( "020-123456"), "Invalid phone NL: too short"); + ok(!method( "020-12345678"), "Invalid phone NL: too long"); ok(!method( "-0201234567"), "Invalid phone NL"); - ok(!method( "+310201234567"), "Invalid phone NL"); // no 0 after +31 allowed + ok(!method( "+310201234567"), "Invalid phone NL: no 0 after +31 allowed"); }); +test("mobileNL", function() { + var method = methodTest("mobileNL"); + ok( method( "0612345678"), "Valid NL Mobile Number"); + ok( method( "06-12345678"), "Valid NL Mobile Number"); + ok( method( "06-12 345 678"), "Valid NL Mobile Number"); + ok( method( "+316-12345678"), "Valid NL Mobile Number"); + ok( method( "+31(0)6-12345678"), "Valid NL Mobile Number"); + ok(!method( "abcdefghij"), "Invalid NL Mobile Number: text"); + ok(!method( "0123456789"), "Invalid NL Mobile Number: should start with 06"); + ok(!method( "0823456789"), "Invalid NL Mobile Number: should start with 06"); + ok(!method( "06-1234567"), "Invalid NL Mobile Number: too short"); + ok(!method( "06-123456789"), "Invalid NL Mobile Number: too long"); + ok(!method( "-0612345678"), "Invalid NL Mobile Number"); + ok(!method( "+310612345678"), "Invalid NL Mobile Number: no 0 after +31 allowed"); +}); + + test("time", function() { var method = methodTest("time"); ok( method("00:00"), "Valid time, lower bound" ); From 600c510dfaf5d95bf8e219fe571f26ca6329dcdf Mon Sep 17 00:00:00 2001 From: Robbert Wethmar Date: Wed, 6 Feb 2013 13:25:45 +0100 Subject: [PATCH 3/6] add dutch postal code validation --- additional-methods.js | 3 +++ localization/messages_nl.js | 3 ++- test/methods.js | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/additional-methods.js b/additional-methods.js index e8963e853..b08b166b6 100644 --- a/additional-methods.js +++ b/additional-methods.js @@ -168,6 +168,9 @@ jQuery.validator.addMethod("mobileNL", function(value, element) { return this.optional(element) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)6((\s|\s?\-\s?)?[0-9]){8}$/.test(value); }, "Please specify a valid mobile number"); +jQuery.validator.addMethod("postalcodeNL", function(value, element) { + return this.optional(element) || /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value); +}, "Please specify a valid postal code"); jQuery.validator.addMethod("time", function(value, element) { return this.optional(element) || /^([01]\d|2[0-3])(:[0-5]\d){1,2}$/.test(value); diff --git a/localization/messages_nl.js b/localization/messages_nl.js index 0dbaad1c2..b99580b7a 100644 --- a/localization/messages_nl.js +++ b/localization/messages_nl.js @@ -25,6 +25,7 @@ // for validations in additional-methods.js dateNL: "Vul hier een geldige datum in.", phoneNL: "Vul hier een geldig Nederlands telefoonnummer in.", - phoneNL: "Vul hier een geldig Nederlands mobiel telefoonnummer in." + phoneNL: "Vul hier een geldig Nederlands mobiel telefoonnummer in.", + postalcodeNL: "Vul hier een geldige postcode in." }); }(jQuery)); \ No newline at end of file diff --git a/test/methods.js b/test/methods.js index 438771c57..1770bf067 100644 --- a/test/methods.js +++ b/test/methods.js @@ -621,6 +621,18 @@ test("mobileNL", function() { ok(!method( "+310612345678"), "Invalid NL Mobile Number: no 0 after +31 allowed"); }); +test("postalcodeNL", function() { + var method = methodTest("postalcodeNL"); + ok( method( "1234AB"), "Valid NL Postal Code"); + ok( method( "1234ab"), "Valid NL Postal Code"); + ok( method( "1234 AB"), "Valid NL Postal Code"); + ok( method( "6789YZ"), "Valid NL Postal Code"); + ok(!method( "123AA"), "Invalid NL Postal Code: not enough digits"); + ok(!method( "12345ZZ"), "Invalid NL Postal Code: too many digits"); + ok(!method( "1234 AA"), "Invalid NL Postal Code: too many spaces"); + ok(!method( "AA1234"), "Invalid NL Postal Code"); + ok(!method( "1234-AA"), "Invalid NL Postal Code"); +}); test("time", function() { var method = methodTest("time"); From 6ad95e00e122e7468b271ac52b5f99cbc20344d8 Mon Sep 17 00:00:00 2001 From: Robbert Wethmar Date: Wed, 6 Feb 2013 15:00:06 +0100 Subject: [PATCH 4/6] add dutch bank and giro account checks --- additional-methods.js | 38 ++++++++++++++++++++++++++++++++++++- localization/messages_nl.js | 5 ++++- test/methods.js | 32 +++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/additional-methods.js b/additional-methods.js index b08b166b6..7b5ef1a84 100644 --- a/additional-methods.js +++ b/additional-methods.js @@ -158,7 +158,7 @@ jQuery.validator.addMethod("dateNL", function(value, element) { }, "Please enter a correct date"); /** - * Dutch phone numbers have 10 digits, or 11 and start with +31. + * Dutch phone numbers have 10 digits (or 11 and start with +31). */ jQuery.validator.addMethod("phoneNL", function(value, element) { return this.optional(element) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9]){8}$/.test(value); @@ -172,6 +172,42 @@ jQuery.validator.addMethod("postalcodeNL", function(value, element) { return this.optional(element) || /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value); }, "Please specify a valid postal code"); +/** + * Dutch bank account numbers (not 'giro' numbers) have 9 digits + * and pass the '11 check'. + * We accept the notation with spaces, as that is common. + * acceptable: 123456789 or 12 34 56 789 + */ +jQuery.validator.addMethod("bankaccountNL", function(value, element) { + if (this.optional(element)) + return true; + if (!(/^[0-9]{9}|([0-9]{2} ){3}[0-9]{3}$/.test(value))) + return false; + // now '11 check' + account = value.replace(/ /g,''); // remove spaces + var sum = 0; + var len = account.length; + for (pos=0; pos Date: Wed, 6 Feb 2013 19:03:44 +0100 Subject: [PATCH 5/6] fixes in specific dutch validations, based on grunt output --- additional-methods.js | 16 +++++++++------- localization/messages_nl.js | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/additional-methods.js b/additional-methods.js index 7b5ef1a84..9eb980404 100644 --- a/additional-methods.js +++ b/additional-methods.js @@ -179,20 +179,22 @@ jQuery.validator.addMethod("postalcodeNL", function(value, element) { * acceptable: 123456789 or 12 34 56 789 */ jQuery.validator.addMethod("bankaccountNL", function(value, element) { - if (this.optional(element)) + if (this.optional(element)) { return true; - if (!(/^[0-9]{9}|([0-9]{2} ){3}[0-9]{3}$/.test(value))) + } + if (!(/^[0-9]{9}|([0-9]{2} ){3}[0-9]{3}$/.test(value))) { return false; + } // now '11 check' - account = value.replace(/ /g,''); // remove spaces + var account = value.replace(/ /g,''); // remove spaces var sum = 0; var len = account.length; - for (pos=0; pos Date: Wed, 6 Feb 2013 19:21:04 +0100 Subject: [PATCH 6/6] added iban validation --- additional-methods.js | 277 ++++++++++++++++++++++++++++++++++++ localization/messages_nl.js | 3 +- test/methods.js | 73 ++++++++++ 3 files changed, 352 insertions(+), 1 deletion(-) diff --git a/additional-methods.js b/additional-methods.js index 9eb980404..9e3fd4836 100644 --- a/additional-methods.js +++ b/additional-methods.js @@ -210,6 +210,283 @@ jQuery.validator.addMethod("bankorgiroaccountNL", function(value, element) { ($.validator.methods["giroaccountNL"].call(this, value, element)); }, "Please specify a valid bank or giro account number"); +/** + * IBAN is the international bank account number. + * It has a country - specific format, that is checked here too + */ +jQuery.validator.addMethod("iban", function(value, element) { + // some quick simple tests to prevent needless work + if (this.optional(element)) { + return true; + } + if (!(/^([a-zA-Z0-9]{4} ){2,8}[a-zA-Z0-9]{1,4}|[a-zA-Z0-9]{12,34}$/.test(value))) { + return false; + } + + // check the country code and find the country specific format + var iban = value.replace(/ /g,'').toUpperCase(); // remove spaces and to upper case + var countrycode = iban.substring(0,2); + var bbanformat = ""; + var bbanpattern = ""; + switch (countrycode) { + case 'AL': + bbanformat = "8n16c"; + break; + case 'AD': + bbanformat = "8n12c"; + break; + case 'AT': + bbanformat = "16n"; + break; + case 'AZ': + bbanformat = "4c20n"; + break; + case 'BE': + bbanformat = "12n"; + break; + case 'BH': + bbanformat = "4a14c"; + break; + case 'BA': + bbanformat = "16n"; + break; + case 'BR': + bbanformat = "23n1a1c"; + break; + case 'BG': + bbanformat = "4a6n8c"; + break; + case 'CR': + bbanformat = "17n"; + break; + case 'HR': + bbanformat = "17n"; + break; + case 'CY': + bbanformat = "8n16c"; + break; + case 'CZ': + bbanformat = "20n"; + break; + case 'DK': + bbanformat = "14n"; + break; + case 'DO': + bbanformat = "4a20n"; + break; + case 'EE': + bbanformat = "16n"; + break; + case 'FO': + bbanformat = "14n"; + break; + case 'FI': + bbanformat = "14n"; + break; + case 'FR': + bbanformat = "10n11c2n"; + break; + case 'GE': + bbanformat = "2c16n"; + break; + case 'DE': + bbanformat = "18n"; + break; + case 'GI': + bbanformat = "4a15c"; + break; + case 'GR': + bbanformat = "7n16c"; + break; + case 'GL': + bbanformat = "14n"; + break; + case 'GT': + bbanformat = "4c20c"; + break; + case 'HU': + bbanformat = "24n"; + break; + case 'IS': + bbanformat = "22n"; + break; + case 'IE': + bbanformat = "4c14n"; + break; + case 'IL': + bbanformat = "19n"; + break; + case 'IT': + bbanformat = "1a10n12c"; + break; + case 'KZ': + bbanformat = "3n13c"; + break; + case 'KW': + bbanformat = "4a22c"; + break; + case 'LV': + bbanformat = "4a13c"; + break; + case 'LB': + bbanformat = "4n20c"; + break; + case 'LI': + bbanformat = "5n12c"; + break; + case 'LT': + bbanformat = "16n"; + break; + case 'LU': + bbanformat = "3n13c"; + break; + case 'MK': + bbanformat = "3n10c2n"; + break; + case 'MT': + bbanformat = "4a5n18c"; + break; + case 'MR': + bbanformat = "23n"; + break; + case 'MU': + bbanformat = "4a19n3a"; + break; + case 'MC': + bbanformat = "10n11c2n"; + break; + case 'MD': + bbanformat = "2c18n"; + break; + case 'ME': + bbanformat = "18n"; + break; + case 'NL': + bbanformat = "4a10n"; + break; + case 'NO': + bbanformat = "11n"; + break; + case 'PK': + bbanformat = "4c16n"; + break; + case 'PS': + bbanformat = "4c21n"; + break; + case 'PL': + bbanformat = "24n"; + break; + case 'PT': + bbanformat = "21n"; + break; + case 'RO': + bbanformat = "4a16c"; + break; + case 'SM': + bbanformat = "1a10n12c"; + break; + case 'SA': + bbanformat = "2n18c"; + break; + case 'RS': + bbanformat = "18n"; + break; + case 'SK': + bbanformat = "20n"; + break; + case 'SI': + bbanformat = "15n"; + break; + case 'ES': + bbanformat = "20n"; + break; + case 'SE': + bbanformat = "20n"; + break; + case 'CH': + bbanformat = "5n12c"; + break; + case 'TN': + bbanformat = "20n"; + break; + case 'TR': + bbanformat = "5n17c"; + break; + case 'AE': + bbanformat = "3n16n"; + break; + case 'GB': + bbanformat = "4a14n"; + break; + case 'VG': + bbanformat = "4c16n"; + break; + } + if (bbanformat === "") { + return false; // unknown country: MAYBE WE SHOULD ALLOW THIS??? + } + + // check the country specific format + while (bbanformat.length>1) { + var count = 0; + var type = "?"; + var l = 0; + if (/^\d[acn]/.test(bbanformat)) { + l = 1; + } else { + l = 2; + } + + count = bbanformat.substring(0, l); + type = bbanformat.substring(l, l + 1); + bbanformat = bbanformat.substring(l + 1, bbanformat.length); + if (count === 0) { + break; // this indicates illegal bbanformat string: MAYBE HANDLE??? + } + var typeexpression = ''; + switch (type) { + case 'a': // alphabet letter + typeexpression = '[A-Z]'; + break; + case 'c': // character (digit or letter) + typeexpression = '[\\dA-Z]'; + break; + case 'n': // number (digit actually) + typeexpression = '\\d'; + break; + } + bbanpattern = bbanpattern + typeexpression + "{" + count + "}"; + } + var ibanregexp = new RegExp("^[A-Z]{2}\\d{2}" + bbanpattern + "$", ""); + if (!(ibanregexp.test(iban))) { + return false; // invalid country specific format + } + + // now check the checksum, first convert to digits + var ibancheck = iban.substring(4,iban.length) + iban.substring(0,4); + var ibancheckdigits = ""; + var leadingZeroes = true; + for (var i =0; i