Validating several fields with the same name using jQuery Validate

后端 未结 1 385
一生所求
一生所求 2020-12-07 03:34

I have an HTML form that I\'m processing with Perl CGI, like so:

相关标签:
1条回答
  • 2020-12-07 04:16

    OFFICIAL DOCUMENTATION: jqueryvalidation.org/reference/#link-markup-recommendations

    "Mandated: A 'name' attribute is required for all input elements needing validation, and the plugin will not work without this. A 'name' attribute must also be unique to the form, as this is how the plugin keeps track of all input elements. However, each group of radio or checkbox elements will share the same 'name' since the value of this grouping represents a single piece of the form data."


    <input type="text" name="foo" class="required" />
    <input type="text" name="foo" class="required" />
    <input type="text" name="foo" class="required" />
    

    Quote OP:

    Trouble is, when I call $('#main').valid(), it only checks that the first field is non-empty. How to I get it to check all the fields?

    It has nothing specifically to do with .valid() and it will fail the same using other jQuery Validate methods. That's because all three fields have the same name of foo. For this plugin each input must have a unique name.

    DEMO: jsfiddle.net/xaFZj/


    EDIT:

    You cannot have duplicate names while using this plugin. This demo clearly shows that the plugin will fail when the name attribute is duplicated. There is no workaround since the name attribute is how the plugin keeps track of the form elements.

    jsfiddle.net/ed3vxgmy/

    The only exception is a radio or checkbox group where the elements in each grouping will share the same name.

    0 讨论(0)
提交回复
热议问题