conditional validation in jquery
I am having two methods to validate the zipcode.
I am having country list box, Here if i select usa i need to validate using usazipRegex method or if i selects canada i need to validate using canadazipRegex . suppose i selects any other country just i need required
//USA zipcode validation methode
jQuery.validator.addMethod("usazipRegex", function(value, element) {
return this.optional(element) || /^[0-9]{5}(?:-[0-9]{4})?$/i.test(value);
}, "You must enter your postal code in this format: #####-####");
//Canada zipcode validation methode
jQuery.validator.addMethod("canadazipRegex", function(v开发者_如何学Calue, element) {
return this.optional(element) || /^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$/i.test(value);
}, "You must enter your postal code in this format: A#A #A#");
validation plugin http://bassistance.de/
How can i do this.Suggest me !
You can toggle a CSS class
on the input when changing the country and then use addClassRules( name, rules )
, as in:
jQuery.validator.addClassRules("usa", {
required: true,
minlength: 10,
usazipRegex: true
});
jQuery.validator.addClassRules("canada", {
required: true,
minlength: 8,
canadaPostalCodeRegex: true
});
Combine with jQuery.validator.addMethod
to add your custom validators usazipRegex
and canadaPostalCodeRegex
.
精彩评论