开发者

jQuery validation: .each with form

开发者 https://www.devze.com 2022-12-23 14:44 出处:网络
If I have a form of about 40 questions, how do I apply the same rules to questions 1-20, and 21-40? For example:

If I have a form of about 40 questions, how do I apply the same rules to questions 1-20, and 21-40?

For example:

$("#form_survey").validate({
    rules: {
        a_ +i: {max:12, maxlength:2},
    },
    messages: {
        a_ +i:{
            max: "That's too much!"
        }

    }   

Where the "+i" is the ideal increment of +开发者_StackOverflow中文版1...

Should be easy, I'm just stuck on syntax...


You could have them all use the same class: class="question". Then, use the class to create the validation:


$(".question").each(function (i) {
  this.validate({
      rules: {
          a_ +i: {max:12, maxlength:2},
      },
      messages: {
          a_ +i:{
              max: "That's too much!"
          }
      } 
  }
});


This solves my problem:

$('.text-input').addClass('hours');
jQuery.validator.addClassRules("hours", {
  required: true,
  minlength: 2
});
0

精彩评论

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