I am new to Jquery and below is the script I have to validate a form on the page:
var rulesObj = {};
var messagesObj = {};
var selectedRows = j$('table[id$="selectedProductsDiv"] tr').has('[id$="checkBoxColumn"] :checkbox:checked');
for(i = 0; i<selectedRows.length; i++){
var element1 = j$(selectedRows[i]).find('input[id$="quantityId"]');
rulesObj[j$(element1).attr("name")] = "required";
messagesObj[j$(element1).attr("name")] = "Field cannot be blank";
var element2 = j$(selectedRows[i]).find('input[id$="serviceDateId"]');
rulesObj[j$(element2).attr("name")] = "required";
messagesObj[j$(element2).attr("name")] = "Field cannot be blank";
}
console.log('Rules object is '+ JSON.stringify(rulesObj, null, 4));
console.log('Messages object is '+ JSON.stringify(messagesObj, null, 4));
j$('form[id$="mainFormId"]').validate({
debug: true,
rules:rulesObj,
messages: messagesObj
});
and this is what prints in the console:
Rules object is {
"j_id0:mainFormId:selected:selectedProductsDiv:3:quantityId": "required",
"j_id0:mainFormId:selected:selectedProductsDiv:3:serviceDateId": "required"
}
Messages object is {
"j_id0:mainFormId:selected:selectedProductsDiv:3:quantityId": "Field cannot be blank",
"j_id0:mainFormId:selected:selectedProductsDiv:3:serviceDateId": "Field cannot be blank"
}
All the fields and the form exist on the page with correct Ids and name, so not sure why the validator is not firing.
Could some body please help me on this?
EDIT: This is how the form looks like and I want the validation to be done only on the selected rows when user clicks on the 'save selected' button. The validation should not be performed when user wants to delete the rows from the table.
The above code I am running when user clicks on the 'save selected' button and not on the 'Delete Selected' button.