2

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/

Sparky
  • 98,165
  • 25
  • 199
  • 285
muni
  • 1,362
  • 2
  • 19
  • 21
  • There is no way to do this without the `name` attribute, period. The jQuery Validate plugin needs the `name` attribute to keep track of the input elements. See: http://stackoverflow.com/questions/20140588/do-i-need-to-use-name-attributes-when-validating-a-bootstrap-form-with-jquery-va/20168104#20168104 – Sparky Dec 01 '13 at 06:44

2 Answers2

2

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

umutm
  • 2,832
  • 4
  • 22
  • 22
  • 1
    It need to add rules for every input fields. But I need modify plugin to validate based on id instead of name. – muni Sep 05 '14 at 03:19
2

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/

Shiva
  • 11,485
  • 2
  • 67
  • 84