1

I have this HTML code:

<section id="variations_holder">
    <input type="text" name="field1" class="pprice" />
    <br/>
    <input type="text" name="field2" class="pprice" />
    <br/>
    <input type="submit" />
</section>

And this is the code I'm using to handle validation:

$('#variations_holder input.pprice').each(function() {
    $(this).rules('add', {
        required: true,
        number: true,
        messages: {
            required: "Debes introducir un precio de la variación",
            number: "El precio de la variación debe ser un valor numérico o decimal"
        }
    });
});

And it works, however when the HTML looks like this (notice input has the same name's):

<section id="variations_holder">
    <input type="text" name="field1" class="pprice" />
    <br/>
    <input type="text" name="field1" class="pprice" />
    <br/>
    <input type="submit" />
</section>

The same code stop working and cause this error:

TypeError: e.validator.methods[o] is undefined

How I can fix this issue?

This is derived from this question

2 Answers 2

9

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:

Sources:

Sign up to request clarification or add additional context in comments.

3 Comments

It's worth noting that radio button inputs can have the same name, as long as class rules are only applied to one of the inputs in the group. See [link] (stackoverflow.com/questions/21405138/…)
@amigolargo, I assume the reader already knows that a "grouping" of radios or checkboxes usually represents a single input point as far as form data is concerned. And the same constraint applies. You cannot have two radio or checkbox groups with the same name.
It was just to clarify in case anyone read this post with the same problem I had been facing - how to apply class rules to radio button groups (only add the class to a single radio button within the group). This is not necessarily immediately apparent - no affront intended
-1

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

Plug-ins used:

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'
        }

    }
});

1 Comment

The require_from_group rule only makes it appear as if your solution is working properly. Otherwise, try using standard rules like required, minlength, etc. and the core issue becomes apparent: jsfiddle.net/he21scmw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.