How can I validate dynamically created text inputs?
I have an “add more” link that creates new ones, and a link to remove them. Each one has a uniqe ID.
Also, at least two text boxes must be filled.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I wrote a blog post explaining how to dynamically add inputs to a form, and simultaneously add validation rules for the jQuery Validate plugin.
In my example, it was an order form for helmets:
The blog post goes into more detail, and also explains how to process this with PHP.
As for the requirement that at least two textboxes must be filled, I wrote a jQuery Validate method for that, and a similar one that says “either fill at least X of the fields in this section, or skip the section entirely.”
So, for example, if your form lets people order sandwiches, and each sandwich has an input for bread and filling, you can dynamically add fields for as many sandwiches as they want to order. They don’t have to fill in anything for sandwich #2, but if they do, they have to give you both the bread AND the filling type.