Skip to content

Commit f8e4be5

Browse files
committed
Merge pull request jquery-validation#1536 from thomasgohard/ca-postal-code-update
Additionals: Updates to postalCodeCA method Allow both upper case and lower case characters and zero or more spaces between the Forward Sorting Area and Local Delivery Unit portions as users do not always use all upper case or include a space (see wet-boew/wet-boew#6760). Correct the regular expression to fail entries that include invalid characters D, F, I, O, Q, and U which are not used in Canadian postal codes.
2 parents b4d8887 + 361d6e8 commit f8e4be5

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/additional/postalCodeCA.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
* @cat Plugins/Validate/Methods
1313
*/
1414
$.validator.addMethod( "postalCodeCA", function( value, element ) {
15-
return this.optional( element ) || /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d$/.test( value );
15+
return this.optional( element ) || /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] *\d[ABCEGHJKLMNPRSTVWXYZ]\d$/i.test( value );
1616
}, "Please specify a valid postal code" );

test/methods.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,12 +1181,16 @@ test("currency", function() { // Works with any symbol
11811181

11821182
test("postalCodeCA", function() {
11831183
var method = methodTest("postalCodeCA");
1184-
ok( method( "H0H 0H0"), "Valid CA Postal Code; Single space" );
1185-
ok( !method( "H0H0H0"), "Inalid CA Postal Code; No space" );
1186-
ok( !method( "H0H-0H0"), "Invalid CA Postal Code; Single dash" );
1187-
ok( !method( "H0H 0H"), "Invalid CA Postal Code; Too Short" );
1188-
ok( !method( "Z0H 0H"), "Invalid CA Postal Code; Only 'ABCEGHJKLMNPRSTVXY' are valid starting characters" );
1189-
ok( !method( "h0h 0h0"), "Invalid CA Postal Code; Only upper case characters" );
1184+
ok( method( "H0H0H0"), "Valid Canadian postal code: all upper case with no space" );
1185+
ok( method( "H0H 0H0"), "Valid Canadian postal code: all upper case with one space" );
1186+
ok( method( "H0H 0H0"), "Valid Canadian postal code: all upper case with multiple spaces" );
1187+
ok( method( "h0h 0h0"), "Valid Canadian postal code: all lower case with space" );
1188+
ok( method( "h0h0h0" ), "Valid Canadian postal code: all lower case with no space" );
1189+
ok( !method( "H0H-0H0"), "Invalid Canadian postal code: dash used as separator" );
1190+
ok( !method( "H0H 0H"), "Invalid Canadian postal code: too short" );
1191+
ok( !method( "Z0H 0H0"), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVXY' are valid first characters for the Forward Sorting Area" );
1192+
ok( !method( "H0D 0H0"), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVWXYZ' are valid third characters for the Forward Sorting Area" );
1193+
ok( !method( "H0H 0D0"), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVWXYZ' are valid second characters for the Local Delivery Unit" );
11901194
});
11911195

11921196
test("stateUS", function() {

0 commit comments

Comments
 (0)