jQuery Validation - require at least one input of the same name

社会主义新天地 提交于 2020-01-24 13:07:48

问题


I'm using the Jquery Validation plugin and have a series of inputs with the same name in an array, for example 5 inputs with name="name[]"

I want to have at least one of these required. Would anyone know how to do this? Thanks!


回答1:


You can state this more generally as "at least X inputs that match selector Y must be filled." (In your case, X is 1 and Y is "with a specific name.")

I addressed the general problem here:

jQuery Validate - require at least one field in a group to be filled




回答2:


you can customize the validation plugin to your needs via the addMethod method from your api

then you can create the exact rule you need to validate your form.




回答3:


You can add a dependency callback to your "requires" rule. It lets you give a function for whether a field is required or not.

Make the first field required unless any of the other fields are filled in.

Do this by making it required, and setting the dependency callback to a function that loops through all the other fields and returns false if any of them has a value. See: http://docs.jquery.com/Plugins/Validation/Methods/required#required.28.C2.A0dependency-callback_.29



来源:https://stackoverflow.com/questions/6061515/jquery-validation-require-at-least-one-input-of-the-same-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!