I have this HTML code:
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...
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:
- "... 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:
- "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:
- "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:
- "... 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
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'
}
}
});