I am facing a problem with jQuery Validation to validate the ajax (dynamically) generated text fields...
Here is the snippet of the HTML code
<form name="booking" id="booking" method="post" action="">
<ul>
<li>
<span>Mobile Number</span>
</li>
<li>
<input type="tel" name="mobile" id="mobile" class='form_mobile'/>
</li>
<li>
<span>Number of male tickets</span>
</li>
<li>
<select name="male_qunatity" id="male_qunatity" >
</select>
</li>
</ul>
</form>
Jquery validation script is here
$(document).ready(function(){
Here is the ajax call starts
$('#male_qunatity').change(function() {
var male_tickets = $(this).val();
var mobile = $('#mobile').val();
var path = '<?php echo site_url() ?>';
ajaxurl = path + '/wp-admin/admin-ajax.php';
var data = {
action: 'get_quantity',
qty: male_tickets,
mobile: mobile
};
$.post(ajaxurl, data, function(response) {
$('#m_attendees').html(response);
});
}); //END dropdown change event
$("#booking").validate({
});
$('.form_mobile').each(function() {
$(this).rules('add', {
required:true,
number:true,
messages: {
required:"Please enter a mobile number",
number:"Only numbers allowed"
}
});
});
});//validation ends
Here is the ajax code to text-boxes based on the count selected
$quantity= $_REQUEST['qty'];
$mobile= $_REQUEST['mobile'];
$pass = "<table>";
for($i =0; $i<$quantity; $i++ ){
$pass .= "<tr>
<td>
<input type='text' id='mobile-".$i."' class='form_mobile' name='mmobile[]'>
</td>
</tr>";
}
$pass .= "</table>";
echo $pass;
FYI: The form validation is working fine for the form fields, except the dynamically created fields..
How to solve this one?
Any suggestions would be greatly appreciated..
.rules('add')
AFTER the fields are dynamically created. – Sparky Jun 19 at 15:54.rules('add')
is called the dynamic fields don't yet exist. – Sparky Jun 19 at 15:59