Skip to content

Instantly share code, notes, and snippets.

@sincarne
Created October 10, 2012 20:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sincarne/3868339 to your computer and use it in GitHub Desktop.
Save sincarne/3868339 to your computer and use it in GitHub Desktop.
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
}
}
});
@prishg
Copy link

prishg commented Jun 29, 2021

Its really helped me a lot. Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment