开发者

Can I use the range method as a class with the jQuery validation plugin?

开发者 https://www.devze.com 2023-02-05 20:08 出处:网络
I have a form that will have input fields added dynamically via JavaScript. Because I don\'t know about all of the fields beforehand, I can\'t define a range restriction like this:

I have a form that will have input fields added dynamically via JavaScript. Because I don't know about all of the fields beforehand, I can't define a range restriction like this:

$("#myform").validate({
  rules: {
    field: {
      required: true,
      range: [13, 23]
    }
  }
});

Source: http://docs.jquery.com/Plugins/Validation/Methods/range#range

For most validation method开发者_C百科s, such as required, I have been adding validation method names as classes like this:

<input type="text" name="text_2" class="required">

Is it possible for me to use range in the same way that I've used required above? Or do I need to create a new method with a regular expression?


If your input name is text_2 be sure to update that in your rules like so

$("#myform").validate({
rules: {
text_2: {
  required: true,
  range: [13, 23]
 }
}
});

or just change your input with the required field

<input type="text" name="text_2" class="required" range="13,23" />


The current version of jQuery validate respects the min and max attributes of the element so you can have a non-required input with range restrictions like this:

<input type="text" name="text_2" class="number" min="13" max="23" />
0

精彩评论

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