Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions additional-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,26 @@ 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" );
ok(!method( "AAAA 9AA" ), "Invalid postcode" ); // Channel Island
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are a non standard UK postcode and does not follow rest of UK

ok(!method( "AA-2640" ), "Invalid postcode" ); // Channel Island
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are a non standard UK postcode and does not follow rest of UK

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" );
Expand Down