From 705e98116cb44ad47b949f99a8827ae5b29c986c Mon Sep 17 00:00:00 2001 From: Thomas Gohard Date: Thu, 23 Jul 2015 14:10:58 -0400 Subject: [PATCH 1/2] Additionals: Make postalCodeCA method more lenient --- src/additional/postalCodeCA.js | 2 +- test/methods.js | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/additional/postalCodeCA.js b/src/additional/postalCodeCA.js index 976b316c9..296406e91 100644 --- a/src/additional/postalCodeCA.js +++ b/src/additional/postalCodeCA.js @@ -12,5 +12,5 @@ * @cat Plugins/Validate/Methods */ $.validator.addMethod( "postalCodeCA", function( value, element ) { - return this.optional( element ) || /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d$/.test( value ); + return this.optional( element ) || /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$/i.test( value ); }, "Please specify a valid postal code" ); diff --git a/test/methods.js b/test/methods.js index 9b95ca797..54b102653 100644 --- a/test/methods.js +++ b/test/methods.js @@ -1181,12 +1181,14 @@ test("currency", function() { // Works with any symbol test("postalCodeCA", function() { var method = methodTest("postalCodeCA"); - ok( method( "H0H 0H0"), "Valid CA Postal Code; Single space" ); - ok( !method( "H0H0H0"), "Inalid CA Postal Code; No space" ); - ok( !method( "H0H-0H0"), "Invalid CA Postal Code; Single dash" ); - ok( !method( "H0H 0H"), "Invalid CA Postal Code; Too Short" ); - ok( !method( "Z0H 0H"), "Invalid CA Postal Code; Only 'ABCEGHJKLMNPRSTVXY' are valid starting characters" ); - ok( !method( "h0h 0h0"), "Invalid CA Postal Code; Only upper case characters" ); + ok( method( "H0H0H0"), "Valid Canadian postal code: all upper case with no space" ); + ok( method( "H0H 0H0"), "Valid Canadian postal code: all upper case with one space" ); + ok( method( "H0H 0H0"), "Valid Canadian postal code: all upper case with multiple spaces" ); + ok( method( "h0h 0h0"), "Valid Canadian postal code: all lower case with space" ); + ok( method( "h0h0h0" ), "Valid Canadian postal code: all lower case with no space" ); + ok( !method( "H0H-0H0"), "Invalid Canadian postal code: dash used as separator" ); + ok( !method( "H0H 0H"), "Invalid Canadian postal code: too short" ); + ok( !method( "Z0H 0H"), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVXY' are valid starting characters" ); }); test("stateUS", function() { From 361d6e8f56e3b741cbfcf5a0d1506d854b0bc91a Mon Sep 17 00:00:00 2001 From: Thomas Gohard Date: Thu, 23 Jul 2015 15:43:51 -0400 Subject: [PATCH 2/2] Additionals: Correct postalCodeCA to exclude invalid combinations --- src/additional/postalCodeCA.js | 2 +- test/methods.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/additional/postalCodeCA.js b/src/additional/postalCodeCA.js index 296406e91..ba3bb4a4e 100644 --- a/src/additional/postalCodeCA.js +++ b/src/additional/postalCodeCA.js @@ -12,5 +12,5 @@ * @cat Plugins/Validate/Methods */ $.validator.addMethod( "postalCodeCA", function( value, element ) { - return this.optional( element ) || /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$/i.test( value ); + return this.optional( element ) || /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] *\d[ABCEGHJKLMNPRSTVWXYZ]\d$/i.test( value ); }, "Please specify a valid postal code" ); diff --git a/test/methods.js b/test/methods.js index 54b102653..c04988464 100644 --- a/test/methods.js +++ b/test/methods.js @@ -1188,7 +1188,9 @@ test("postalCodeCA", function() { ok( method( "h0h0h0" ), "Valid Canadian postal code: all lower case with no space" ); ok( !method( "H0H-0H0"), "Invalid Canadian postal code: dash used as separator" ); ok( !method( "H0H 0H"), "Invalid Canadian postal code: too short" ); - ok( !method( "Z0H 0H"), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVXY' are valid starting characters" ); + ok( !method( "Z0H 0H0"), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVXY' are valid first characters for the Forward Sorting Area" ); + ok( !method( "H0D 0H0"), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVWXYZ' are valid third characters for the Forward Sorting Area" ); + ok( !method( "H0H 0D0"), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVWXYZ' are valid second characters for the Local Delivery Unit" ); }); test("stateUS", function() {