Created
October 10, 2012 20:51
-
-
Save sincarne/3868339 to your computer and use it in GitHub Desktop.
Validate a Canadian postal code with JQuery Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
Its really helped me a lot. Thank you!!