I have a form where a user can keep adding items to a list. When they go to submit this page, I want to validate that this list actually has items in it (not the individual items as they've already been validated). Each item gets added to a new row in a table with the TR having an extra attribute of "action"... so it looks like:
<tr action="whatever">...</tr>
What I was attempting to do is add a custom addMethod that called a function which would count the number of rows with action as an attribute:
$("#tableID").find("tr[action]").length
and if that length is greater than 0, it returns true, otherwise, false.
This works fine outside of the validator calls but for some reason it completely skips over it.
I could really use an example or some insight into how to make it validate this rule even though it is not a form element specifically.
Scaled down code:
*note that I already have defaults being setup for messages and what not.
$.validator.addMethod("validProductList", function (value, element) {
return this.optional(element) || validateProductList();
}, "You have no products in your list");
$("#processForm").click(function () {
$("#pageForm").validate({
submitHandler: function () {
$("#errors").hide();
//processPage();
},
rules: {
//other rules,
validProductList: true
}
});
});
function validateProductList() {
var isValid = false;
var useList = $("#tblAddedProducts").find("tr[action]").length;
if (useList > 0) { isValid = true; }
return isValid;
}