Jquery validator not firing

╄→гoц情女王★ 提交于 2019-12-13 05:54:52

问题


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.

来源:https://stackoverflow.com/questions/31681421/jquery-validator-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!