简体   繁体   中英

validating form fields using jquery validation plugin?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM