开发者

conditional validation in jquery

开发者 https://www.devze.com 2023-03-04 03:15 出处:网络
I am having two methods to validate the zipcode. I am having country list box, Here if i select usa i need to validate usingusazipRegex method or if i selects canada i need to validate using canadazi

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消