I have a set of inputs (text boxes) which are added dynamically with the means of AJAX ($.post()). These input boxes have to be validated too. The validation works, but I can't seem to add any classes to the inputbox
$("input[id^=code]").each(function() {
if (!$.isNumeric($(this).val()) || $(this).val().length !== 6) {
$(this).addClass("invalid");
}
})
The input boxes start with the id code and go like: code1, code2, code3
The html:
<div id="shoe_codes_maininput" style="display: table-cell;">
<div id="shoe_codes_row_1">
<label class="shoe_code_label required">Shoe Code 1:<span class="star"> *</span></label>
<div class="shoe_code_input"><input placeholder="Enter the shoe code" id="code1" name="shoe[code_1]" class="required" maxlength="6"> </div>
</div>
<div id="shoe_codes_row_2">
<label class="shoe_code_label required">Shoe Code 2:<span class="star"> *</span></label>
<div class="shoe_code_input"><input placeholder="Enter the shoe code" id="code2" name="shoe[code_2]" class="required" maxlength="6"> </div>
</div>
</div>
The Joomla submit function:
$(document).ready(function() {
Joomla.submitbutton = function(task)
{
if (task == 'order.cancel') {
Joomla.submitform(task, document.getElementById('order-form'));
}
else {
var order = true;
$("input[id^=code]").each(function() {
if (!$.isNumeric($(this).val()) || $(this).val().length !== 6) {
$(this).addClass("invalid");
order = false;
}
})
if (task != 'order.cancel' && document.formvalidator.isValid(document.id('order-form')) && order) {
Joomla.submitform(task, document.getElementById('order-form'));
}
else {
alert('<?php echo $this->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED')); ?>');
}
}
}
});