Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validate a Canadian postal code with JQuery Validation
// Validate a Canadian postal code with JQuery Validation
// Use with http://docs.jquery.com/Plugins/Validation
// Adding the method
jQuery.validator.addMethod("cdnPostal", function(postal, element) {
return this.optional(element) ||
postal.match(/[a-zA-Z][0-9][a-zA-Z](-| |)[0-9][a-zA-Z][0-9]/);
}, "Please specify a valid postal code.");
// Using the new rule
$("#form").validate({
rules: {
postal_code: {
required: true,
cdnPostal: true
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.