0

Still is not clear to me how I can validate a dynamic form fields using jQuery validate ()

Suppose I have a form like the following:

<form id="myform" action="" ...>
   <input type="text" form-control" name="cauthor[]" id="cauthor1" value=""/>
....

  <button type="button" class="clone btn btn-primary" value="Add">
 ....
  <button type="submit" name="submit" value="submit">
...
</form>

As you can see I have a dynamic field (cauthor[]); So user can hit the Add button to create a new input field. Then after hitting the Add button my form will look like this:

   <input type="text" form-control" name="cauthor[]" id="cauthor1" value=""/>
   <input type="text" form-control" name="cauthor[]" id="cauthor1" value=""/>
   <input type="text" form-control" name="cauthor[]" id="cauthor1" value=""/>

Now the question is? How to validate all the fields?

$("#casos_clinicos_form").validate({
    rules: {
        'cauthor[]': {
            required: true
        }
    },
    messages: {
        'cauthor[]': "Author name not informed! Please correct and try again."
    }

});

It only validates the first one.

If you know how to handle the above, please reply.

Sparky
  • 98,165
  • 25
  • 199
  • 285
user1818765
  • 23
  • 1
  • 5
  • You can assign a class to these controls then validation will work – Rashid Javed Aug 15 '16 at 16:30
  • @RashidJaved, absolutely wrong. This plugin mandates that all input nodes contain a unique `name`. Using your approach, only the first instance will be validated. http://jsfiddle.net/mcqb2vk2/ – Sparky Aug 15 '16 at 17:16

1 Answers1

-2

You can assign a class to input and then validate it.

Query.validator.addClassRules('myClassName', {
        required: true /*,
        other rules */
    });