Browse files

Improved the UK postcode method to filter out more invalid postcodes.…

… Closes #682

Old one allows integers between 5-7 digits which is not valid.

New one is still case insensitive and allows spaces & fixes the above issue.
  • Loading branch information...
1 parent cc810a2 commit bbbf87903da3a032432cd8bfac989733040b48d5 James Thompson committed with Mar 12, 2013
Showing with 27 additions and 5 deletions.
  1. +4 −5 additional-methods.js
  2. +23 −0 test/methods.js
View
9 additional-methods.js
@@ -389,11 +389,10 @@ jQuery.validator.addMethod('phonesUK', function(phone_number, element) {
// A number of very detailed GB telephone number RegEx patterns can also be found at:
// http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers
-//Matches UK postcode. based on http://snipplr.com/view/3152/postcode-validation/
-jQuery.validator.addMethod('postcodeUK', function(postcode, element) {
- postcode = (postcode.toUpperCase()).replace(/\s+/g,'');
- return this.optional(element) || postcode.match(/^([^QZ][^IJZ]{0,1}\d{1,2})(\d[^CIKMOV]{2})$/) || postcode.match(/^([^QV]\d[ABCDEFGHJKSTUW])(\d[^CIKMOV]{2})$/) || postcode.match(/^([^QV][^IJZ]\d[ABEHMNPRVWXY])(\d[^CIKMOV]{2})$/) || postcode.match(/^(GIR)(0AA)$/) || postcode.match(/^(BFPO)(\d{1,4})$/) || postcode.match(/^(BFPO)(C\/O\d{1,3})$/);
-}, 'Please specify a valid postcode');
+// Matches UK postcode. Does not match to UK Channel Islands that have their own postcodes (non standard UK)
+jQuery.validator.addMethod('postcodeUK', function(value, element) {
+ return this.optional(element) || /^((([A-PR-UWYZ][0-9])|([A-PR-UWYZ][0-9][0-9])|([A-PR-UWYZ][A-HK-Y][0-9])|([A-PR-UWYZ][A-HK-Y][0-9][0-9])|([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))\s?([0-9][ABD-HJLNP-UW-Z]{2})|(GIR)\s?(0AA))$/i.test(value);
+}, 'Please specify a valid UK postcode');
// TODO check if value starts with <, otherwise don't try stripping anything
jQuery.validator.addMethod("strippedminlength", function(value, element, param) {
View
23 test/methods.js
@@ -662,6 +662,29 @@ test("iban", function() {
ok( method( "GB29 NWBK 6016 1331 9268 19"), "Valid IBAN - GB");
});
+test("postcodeUK", function() {
+ var method = methodTest("postcodeUK");
+ ok( method( "AA9A 9AA" ), "Valid postcode" );
+ ok( method( "A9A 9AA" ), "Valid postcode" );
+ ok( method( "A9 9AA" ), "Valid postcode" );
+ ok( method( "A99 9AA" ), "Valid postcode" );
+ ok( method( "AA9 9AA" ), "Valid postcode" );
+ ok( method( "AA99 9AA" ), "Valid postcode" );
+
+ // Channel Island
+ ok(!method( "AAAA 9AA" ), "Invalid postcode" );
+ ok(!method( "AA-2640" ), "Invalid postcode" );
+
+ ok(!method( "AAA AAA" ), "Invalid postcode" );
+ ok(!method( "AA AAAA" ), "Invalid postcode" );
+ ok(!method( "A AAAA" ), "Invalid postcode" );
+ ok(!method( "AAAAA" ), "Invalid postcode" );
+ ok(!method( "999 999" ), "Invalid postcode" );
+ ok(!method( "99 9999" ), "Invalid postcode" );
+ ok(!method( "9 9999" ), "Invalid postcode" );
+ ok(!method( "99999" ), "Invalid postcode" );
+});
+
test("dateNL", function() {
var method = methodTest("dateNL");
ok( method( "01-01-1900" ), "Valid date NL" );

0 comments on commit bbbf879

Please sign in to comment.