Permalink
Browse files

Add additional, dutch-specific methods: phone, mobile, postal, bank a…

…nd giro account

add dutch phone number validation
  • Loading branch information...
1 parent 581ccd8 commit 509e6e3619552427b06c7eb5075834966ab40c0c @digitalica digitalica committed with Feb 6, 2013
Showing with 157 additions and 20 deletions.
  1. +58 −4 additional-methods.js
  2. +9 −2 localization/messages_nl.js
  3. +90 −14 test/methods.js
View
62 additional-methods.js
@@ -153,10 +153,6 @@ jQuery.validator.addMethod("dateITA", function(value, element) {
return this.optional(element) || check;
}, "Please enter a correct date");
-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.");
-
/**
* IBAN is the international bank account number.
* It has a country - specific format, that is checked here too
@@ -279,6 +275,64 @@ jQuery.validator.addMethod("iban", function(value, element) {
return cRest === 1;
}, "Please specify a valid IBAN");
+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);
+}, "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("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");
+
+/*
+ * 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'
+ var account = value.replace(/ /g,''); // remove spaces
+ var sum = 0;
+ var len = account.length;
+ for (var pos=0; pos<len; pos++) {
+ var factor = len - pos;
+ var digit = account.substring(pos, pos+1);
+ sum = sum + factor * digit;
+ }
+ return sum % 11 === 0;
+}, "Please specify a valid bank account number");
+
+/**
+ * Dutch giro account numbers (not bank numbers) have max 7 digits
+ */
+jQuery.validator.addMethod("giroaccountNL", function(value, element) {
+ return this.optional(element) || /^[0-9]{1,7}$/.test(value);
+}, "Please specify a valid giro account number");
+
+jQuery.validator.addMethod("bankorgiroaccountNL", function(value, element) {
+ return this.optional(element) ||
+ ($.validator.methods["bankaccountNL"].call(this, value, element)) ||
+ ($.validator.methods["giroaccountNL"].call(this, value, element));
+}, "Please specify a valid bank or giro account 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");
View
11 localization/messages_nl.js
@@ -21,8 +21,15 @@
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}."),
-
+
// for validations in additional-methods.js
- iban: "Vul hier een geldig IBAN in."
+ iban: "Vul hier een geldig IBAN in.",
+ dateNL: "Vul hier een geldige datum in.",
+ phoneNL: "Vul hier een geldig Nederlands telefoonnummer in.",
+ mobileNL: "Vul hier een geldig Nederlands mobiel telefoonnummer in.",
+ postalcodeNL: "Vul hier een geldige postcode in.",
+ bankaccountNL: "Vul hier een geldig bankrekeningnummer in.",
+ giroaccountNL: "Vul hier een geldig gironummer in.",
+ bankorgiroaccountNL: "Vul hier een geldig bank- of gironummer in."
});
}(jQuery));
View
104 test/methods.js
@@ -585,20 +585,6 @@ test("dateITA", function() {
ok(!method( "01/01/199" ), "Invalid date ITA" );
});
-test("dateNL", function() {
- var method = methodTest("dateNL");
- ok( method( "01-01-1900" ), "Valid date NL" );
- ok( method( "01.01.1900" ), "Valid date NL" );
- ok( method( "01/01/1900" ), "Valid date NL" );
- ok( method( "01-01-00" ), "Valid date NL" );
- ok( method( "1-01-1900" ), "Valid date NL" );
- ok( method( "10-10-1900" ), "Valid date NL" );
- ok(!method( "0-01-1900" ), "Invalid date NL" );
- ok(!method( "00-01-1900" ), "Invalid date NL" );
- ok(!method( "35-01-1990" ), "Invalid date NL" );
- ok(!method( "01.01.190" ), "Invalid date NL" );
-});
-
test("iban", function() {
var method = methodTest("iban");
ok( method( "NL20INGB0001234567"), "Valid IBAN");
@@ -676,6 +662,96 @@ test("iban", function() {
ok( method( "GB29 NWBK 6016 1331 9268 19"), "Valid IBAN - GB");
});
+test("dateNL", function() {
+ var method = methodTest("dateNL");
+ ok( method( "01-01-1900" ), "Valid date NL" );
+ ok( method( "01.01.1900" ), "Valid date NL" );
+ ok( method( "01/01/1900" ), "Valid date NL" );
+ ok( method( "01-01-00" ), "Valid date NL" );
+ ok( method( "1-01-1900" ), "Valid date NL" );
+ ok( method( "10-10-1900" ), "Valid date NL" );
+ ok(!method( "0-01-1900" ), "Invalid date NL" );
+ ok(!method( "00-01-1900" ), "Invalid date NL" );
+ ok(!method( "35-01-1990" ), "Invalid date NL" );
+ 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("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("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("bankaccountNL", function() {
+ var method = methodTest("bankaccountNL");
+ ok( method( "755490975"), "Valid NL bank account");
+ ok( method( "75 54 90 975"), "Valid NL bank account");
+ ok( method( "123456789"), "Valid NL bank account");
+ ok( method( "12 34 56 789"), "Valid NL bank account");
+ ok(!method( "12 3456789"), "Valid NL bank account: inconsistent spaces");
+ ok(!method( "123 45 67 89"), "Valid NL bank account: incorrect spaces");
+ ok(!method( "755490971"), "Invalid NL bank account");
+ ok(!method( "755490973"), "Invalid NL bank account");
+ ok(!method( "755490979"), "Invalid NL bank account");
+ ok(!method( "123456781"), "Invalid NL bank account");
+ ok(!method( "123456784"), "Invalid NL bank account");
+ ok(!method( "123456788"), "Invalid NL bank account");
+});
+
+test("giroaccountNL", function() {
+ var method = methodTest("giroaccountNL");
+ ok( method( "123"), "Valid NL giro account");
+ ok( method( "1234567"), "Valid NL giro account");
+ ok(!method( "123456788"), "Invalid NL giro account");
+});
+
+test("bankorgiroaccountNL", function() {
+ var method = methodTest("bankorgiroaccountNL");
+ ok( method( "123"), "Valid NL giro account");
+ ok( method( "1234567"), "Valid NL giro account");
+ ok( method( "123456789"), "Valid NL bank account");
+ ok(!method( "12345678"), "Invalid NL bank or giro account");
+ ok(!method( "123456788"), "Invalid NL bank or giro account");
+});
+
test("time", function() {
var method = methodTest("time");
ok( method( "00:00" ), "Valid time, lower bound" );

0 comments on commit 509e6e3

Please sign in to comment.