validating multiple textbox having same name with jquery validator validates only first input

“I am trying to validate the two inputs having same name with jquery
validator …”

<input type="text" name="inp_text"/>
<input type="text" name="inp_text"/>

“but when I run this code it only validates first inputs and second
inputs gets simply ignored”

You cannot have two input fields of type="text" with the same name or this plugin will not work properly. The name attribute must be unique. (One exception to the name being unique, is that “groupings” of checkbox or radio inputs will share the same name as the corresponding submission is a single point of data. However, the name must still be unique to each grouping of checkbox and radio elements.)

“what should I change…?

Make each name attribute unique.

<input type="text" name="inp_text[1]"/>
<input type="text" name="inp_text[2]"/>

Then use the “starts with” selector, ^=

$("[name^=inp_text]").each(function () {
    $(this).rules("add", {
        required: true,
        checkValue: true
    });
});

Working DEMO: http://jsfiddle.net/PgLh3/

NOTES: You can also target elements by id when using the rules('add') method, however for this case, nothing is solved because the plugin still requires a unique name on each input element.

Leave a Comment