0

I am using JQuery validation plugin.

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js"></script>

Today I ran into an issue where I needed to validated fields with the same name, something like: <input type="text" name="foo[]" value="" /> - the problem is the plugin will only validate the first field. I know the reason is because plugin expects unique names i think. Is there any solution to validate all the fields with the same name?

Thanks!

user1016403
  • 12,151
  • 35
  • 108
  • 137
  • if possible please provide a fiddle – Pandiyan Cool Aug 20 '13 at 13:10
  • possible duplicate of [Using JQuery Validate Plugin to validate multiple form fields with identical names](http://stackoverflow.com/questions/931687/using-jquery-validate-plugin-to-validate-multiple-form-fields-with-identical-nam) – jbl Aug 20 '13 at 13:24
  • Why can't you give them unique names? And no, there is no workaround. They must be named uniquely. – Sparky Aug 20 '13 at 13:24
  • have you considered using `$.each`? – Philipp Sander Aug 20 '13 at 13:37
  • There is no workaround. Every field must have a unique name in order for this plugin to function properly. See: http://jsfiddle.net/GuG7x/ – Sparky Aug 20 '13 at 14:08

1 Answers1

0

Quote:

"...the problem is the plugin will only validate the first field. I know the reason is because plugin expects unique names I think. Is there any solution to validate all the fields with the same name?"

This is true. The plugin will not operate properly without a unique name on each field.

Also, using a jQuery $.each() to assign rules via the .rules('add') method is not a viable workaround. That's because the root problem is not with selecting elements & assigning rules, the problem is that the plugin uses the name attribute to keep track of the elements being validated. This is impossible if the name is the same on all.

failed jQuery .each() demo

Sparky
  • 98,165
  • 25
  • 199
  • 285