muni
muni

Reputation: 1360

Validation based on field id rather than field name with jQuery Validation plugin

By default jQuery validation plugin validates form fields based on name of the field.

I want the plugin need validate based on field id.

http://bassistance.de/2013/03/22/release-validation-plugin-1-11-1/

Upvotes: 2

Views: 13682

Answers (2)

Shiva
Shiva

Reputation: 12562

One thing is sure that the name of the input fields must be unique other wise validation is gonna apply to single field only.

you can use item index ( random or serial numbers ) to make the the names unique.

<form id="myform">
    <input type="text" name="bids[spec_attributes][0][name]" /><br/>
    <input type="text"  name="bids[spec_attributes][1][size]"/><br/>
    <input type="text"  name="bids[spec_attributes][2][email]"/><br/>

    <input type="text" name="bids[spec_attributes][10][name]" /><br/>
    <input type="text"  name="bids[spec_attributes][11][size]"/><br/>
    <input type="text"  name="bids[spec_attributes][12][email]"/><br/>
    <input type="submit" />
</form>

and in JS

$(document).ready(function () {

    $('#myform').validate();
    $('input[type="text"]').each(function () {
      $(this).rules('add', {
          required: true
      });
    });

});

have a look to this fiddle http://jsfiddle.net/cjqvnnho/1/

Upvotes: 2

umutm
umutm

Reputation: 2894

It is possible through the add rule method: http://jqueryvalidation.org/rules/

Upvotes: 2

Related Questions