Skip to content

Commit bbbf879

Browse files
James Thompsonjzaefferer
authored andcommitted
Improved the UK postcode method to filter out more invalid postcodes. Closes jquery-validation#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.
1 parent cc810a2 commit bbbf879

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

additional-methods.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,10 @@ jQuery.validator.addMethod('phonesUK', function(phone_number, element) {
389389
// A number of very detailed GB telephone number RegEx patterns can also be found at:
390390
// http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers
391391

392-
//Matches UK postcode. based on http://snipplr.com/view/3152/postcode-validation/
393-
jQuery.validator.addMethod('postcodeUK', function(postcode, element) {
394-
postcode = (postcode.toUpperCase()).replace(/\s+/g,'');
395-
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})$/);
396-
}, 'Please specify a valid postcode');
392+
// Matches UK postcode. Does not match to UK Channel Islands that have their own postcodes (non standard UK)
393+
jQuery.validator.addMethod('postcodeUK', function(value, element) {
394+
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);
395+
}, 'Please specify a valid UK postcode');
397396

398397
// TODO check if value starts with <, otherwise don't try stripping anything
399398
jQuery.validator.addMethod("strippedminlength", function(value, element, param) {

test/methods.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,29 @@ test("iban", function() {
662662
ok( method( "GB29 NWBK 6016 1331 9268 19"), "Valid IBAN - GB");
663663
});
664664

665+
test("postcodeUK", function() {
666+
var method = methodTest("postcodeUK");
667+
ok( method( "AA9A 9AA" ), "Valid postcode" );
668+
ok( method( "A9A 9AA" ), "Valid postcode" );
669+
ok( method( "A9 9AA" ), "Valid postcode" );
670+
ok( method( "A99 9AA" ), "Valid postcode" );
671+
ok( method( "AA9 9AA" ), "Valid postcode" );
672+
ok( method( "AA99 9AA" ), "Valid postcode" );
673+
674+
// Channel Island
675+
ok(!method( "AAAA 9AA" ), "Invalid postcode" );
676+
ok(!method( "AA-2640" ), "Invalid postcode" );
677+
678+
ok(!method( "AAA AAA" ), "Invalid postcode" );
679+
ok(!method( "AA AAAA" ), "Invalid postcode" );
680+
ok(!method( "A AAAA" ), "Invalid postcode" );
681+
ok(!method( "AAAAA" ), "Invalid postcode" );
682+
ok(!method( "999 999" ), "Invalid postcode" );
683+
ok(!method( "99 9999" ), "Invalid postcode" );
684+
ok(!method( "9 9999" ), "Invalid postcode" );
685+
ok(!method( "99999" ), "Invalid postcode" );
686+
});
687+
665688
test("dateNL", function() {
666689
var method = methodTest("dateNL");
667690
ok( method( "01-01-1900" ), "Valid date NL" );

0 commit comments

Comments
 (0)