最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery validate plugin - can't validate classes - Stack Overflow

programmeradmin5浏览0评论

I'm trying to use the jQuery validate plugin to validate classes instead of ID's. Despite the many threads which seem close to answering this issue - I can't get any of them to work. I simply have a form that has a lot of dynamically generated repeating form fields, so naturally I can't add rules for the ID's because there's no knowing how many of them there will be. So, instead, I would like to just target the class on the input field.

<input type="text" id="name1" class="fileName" />
<input type="text" id="name2" class="fileName" />
<input type="text" id="name3" class="fileName" />
<input type="text" id="name4" class="fileName" />

How do I target 'fileName' class? I've tried this:

$(document).ready(function(){
    $.validator.addClassRules({
        fileName:{
        required: true
    }
    });
    $("#myForm").validate();
});

But this does not work at all :(

Any ideas? Anyone?

I'm trying to use the jQuery validate plugin to validate classes instead of ID's. Despite the many threads which seem close to answering this issue - I can't get any of them to work. I simply have a form that has a lot of dynamically generated repeating form fields, so naturally I can't add rules for the ID's because there's no knowing how many of them there will be. So, instead, I would like to just target the class on the input field.

<input type="text" id="name1" class="fileName" />
<input type="text" id="name2" class="fileName" />
<input type="text" id="name3" class="fileName" />
<input type="text" id="name4" class="fileName" />

How do I target 'fileName' class? I've tried this:

$(document).ready(function(){
    $.validator.addClassRules({
        fileName:{
        required: true
    }
    });
    $("#myForm").validate();
});

But this does not work at all :(

Any ideas? Anyone?

Share Improve this question edited Sep 26, 2012 at 2:11 Eli 14.8k5 gold badges61 silver badges77 bronze badges asked Apr 21, 2011 at 18:37 flyagaricusflyagaricus 1191 gold badge2 silver badges8 bronze badges 1
  • where is the rest of your form ? – mcgrailm Commented Apr 21, 2011 at 18:56
Add a ment  | 

3 Answers 3

Reset to default 10

You need to specify a name attribute on each input element for validator to pick it up:

<form action="#">
    <input type="text" id="name1" class="fileName" name="name1" />
    <input type="text" id="name2" class="fileName" name="name2"/>
    <input type="text" id="name3" class="fileName" name="name3"/>
    <input type="text" id="name4" class="fileName" name="name4"/>
</form>

Here's a working example: http://jsfiddle/Nbcj9/

According to the docs, this should work:

jQuery.validator.addClassRules('classNameHere', {
  required: true
});

Then you can determine whether the fields are valid by calling:

$('.fileName').valid();

Here's a link to the docs

what about:

$(".fileName").validate();
发布评论

评论列表(0)

  1. 暂无评论