Skip to content

New Method: postalCodeCA #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
16 changes: 16 additions & 0 deletions src/additional/postalCodeCA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Matches a valid Canadian Postal Code
*
* @example jQuery.validator.methods.postalCodeCA( "H0H 0H0", element )
* @result true
*
* @example jQuery.validator.methods.postalCodeCA( "H0H0H0", element )
* @result false
*
* @name jQuery.validator.methods.postalCodeCA
* @type Boolean
* @cat Plugins/Validate/Methods
*/
jQuery.validator.addMethod( "postalCodeCA", function( value, element ) {
return this.optional( element ) || /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d$/.test( value );
}, "Please specify a valid postal code" );
3 changes: 2 additions & 1 deletion src/localization/messages_fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
require_from_group: "Veuillez fournir au moins {0} de ces champs.",
nifES: "Veuillez fournir un numéro NIF valide.",
nieES: "Veuillez fournir un numéro NIE valide.",
cifES: "Veuillez fournir un numéro CIF valide."
cifES: "Veuillez fournir un numéro CIF valide.",
postalCodeCA: "Veuillez fournir un code postal valide.",
});
}(jQuery));
10 changes: 10 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,4 +1205,14 @@ test("currency", function() { // Works with any symbol
ok(!method( "9.99,9", "£"), "Invalid currency" );
});

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" );
});

})(jQuery);