For those of you who may be familiar with Jörn Zaefferer’s jQuery Validation plugin, I’ve written a method for validating zip/postal code fields, which was not included in the plugin.
To start validating zip/postal code fields, include this code somewhere after you’ve included the plugin:
jQuery.validator.addMethod("postalcode", function(postalcode, element) {
return this.optional(element) || postalcode.match(/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnpstvxy]{1}\d{1}[A-Za-z]{1} ?\d{1}[A-Za-z]{1}\d{1})$/);
}, "Please specify a valid postal/zip code");
Now you can add postalcode to your validation rules like so:
$("#myform").validate({
rules: {
postalcode: {
postalcode: true
}
}
});
Note: this will accept both Canadian postal codes (with or without a space) and U.S. zip codes. Enjoy!

8 Comments to “jQuery Zip/Postal Code Validation Method”
Awesome, thanks
fine. but my postal code is optional field and have to check only format.
how to do that
Works great – thank you!!
The letters D, F, I, O, Q, or U aren’t used in Canadian Postal Codes. So it looks like M2F 1D6 would validate using the code above.
Thanks for this! I was going to write one myself but though why re-invent it if the code already exists
thanks for posting it. Plus my regular expression experience is not very vast.
P.s. if you can post this in my previous comment… here is some code added to your function that will automatically add in a space if someone omits it.
if(postalcode.length == 6 && !parseInt(postalcode)){
// no space in postal code
var s = postalcode.substring(0,3) + ‘ ‘ + postalcode.substring(3);
element.value = s;
}
It looks like the validation method is missing the lowercase r.
isn’t working for me for some reason added it to document.ready in my scripts page below the validator plug in and the postal code is not validating have listed the site if you choose new york and then queens you can see full form