I’m using the jQuery validation plugin to validate a form. The form fields are generated dynamically. I have an email:
<input type="text" name="customer_email_<?=$i?>" value="" id="customer_email_<?=$i?>" />
This input field is in a php loop and can be generated n-number of times. The idea is to check if the entered e-mail is unique. When the user enters something in any of the fields generated I’m sending an AJAX call that is checking in the database if the e-mail is unique.
var pass_count_array = [<?=$js_array?>]; // generated by php
jQuery.each(pass_count_array, function(index, p) {
//check if the e-mail is unique
jQuery.validator.addMethod("unique_email_"+p, function(value, element) {
return check_unique_email(p);
});
var current_email = "customer_email_"+p;
jQuery("#tair_form").validate({
rules : {
current_email: {
"unique_email_"+p : true
}
});
});
The code above is looping through all the fields, adding a custom validation method unique_email_1, unique_email_2, etc.
The problem here is that the validation rules doesn’t accept this:
"unique_email_"+p : true
Any ideas how to apply the custom validation method to every e-mail input field?
Attach a class to the email fields so that you don’t have to search by id but you group by class, that way you just apply a check to the whole class objects.
Loop through them
.each()for instance, and set off alerts as needed.