JQuery validate plugin - can't validate classes

前端 未结 3 1613
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 07:10

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 t

相关标签:
3条回答
  • 2021-01-03 07:51

    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.net/Nbcj9/

    0 讨论(0)
  • 2021-01-03 07:51

    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

    0 讨论(0)
  • 2021-01-03 07:54

    what about:

    $(".fileName").validate();
    
    0 讨论(0)
提交回复
热议问题