Validation of fields with same name and class get “TypeError: e.validator.methods[o] is undefined”

前端 未结 2 1277
耶瑟儿~
耶瑟儿~ 2020-12-20 06:35

I have this HTML code:


相关标签:
2条回答
  • 2020-12-20 06:55

    Quote OP:

    "... the HTML looks like this (notice input has the same name's):"

    <input type="text" name="field1" class="pprice" />
    <input type="text" name="field1" class="pprice" />
    

    "How I can fix this issue?"

    For the fifth time...

    You can NOT have multiple input elements with the same value for every name attribute.

    Because the plugin will NOT be able to keep track of the elements; there is no solution and no workaround for this specification.

    (a collection or "group" of checkbox or radio elements is considered as "one data input", so they can share a name but only within the group representing this single piece of form data.)


    Quote OP:

    "This is derived from this question"

    Where in my answer to that same question, I said:

    1. "... the jQuery Validate plugin requires that each input element contain a unique name attribute. This is how the plugin keeps track of elements. ..."

    as well as in the very second comment on the OP, I said:

    1. "You cannot have multiple elements with same name. jQuery Validate requires that the name be unique."

    and in a comment on my answer, I said:

    1. "However, each element must contain a unique name, no matter how the rules are created and applied."

    and in my last comment on the OP, I said:

    1. "... The jQuery Validate plugin needs unique names on data input elements because that's how the plugin keeps track of them internally. There is no way around this requirement."

    Also see:

    • https://stackoverflow.com/a/19280015/594235

    • https://stackoverflow.com/a/15745248/594235

    • https://stackoverflow.com/a/17643385/594235

    • https://stackoverflow.com/a/16658340/594235

    • https://stackoverflow.com/a/18022304/594235

    • https://stackoverflow.com/a/15162231/594235

    • https://stackoverflow.com/a/18963614/594235

    • https://stackoverflow.com/a/16816867/594235

    • https://stackoverflow.com/a/18022304/594235

    • https://stackoverflow.com/a/18905194/594235

    Sources:

    • jQuery Validate Tag Wiki

    • https://stackoverflow.com/search?q=user:594235 [jquery-validate] unique name

    0 讨论(0)
  • 2020-12-20 07:12

    I had the same problem and the solution in your case would be the following.

    Plug-ins used:

    • http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js
    • http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/additional-methods.min.js

    HTML:

    <input type="text" name="field1[]" class="pprice-group" />
    <input type="text" name="field1[]" class="pprice-group" />
    

    JS:

    $("#form").validate({
        // Rules for form validation
        rules:
        {
            "field1[]":
            {
                require_from_group: [1, ".pprice-group"]
            }
        },
    
        // Messages for form validation
        messages:
        {
            "field1[]":
            {
                require_from_group: 'At least {0} field required'
            }
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题