I am using the jquery validate plugin to validate all the fields on my form, without issue.
However, I have a need to validate the form against the number of rows in an html table located outside the form markup.
Here are the elements in question:
- form: id = "roadForm"
- select input: id = "editRoad_ProjectClassification"
- table: id = "dataExistingSegments"
My business rule goes something like this:
- If the value of "editRoad_ProjectClassification" select is equal to "NO TREATMENTS", then the number of rows in the table "dataExistingSegments" must be equal to zero.
- If the value of "editRoad_ProjectClassification" select is not equal to "NO TREATMENTS", then the number of rows in the table "dataExistingSegments" must be greater than zero.
I have created two custom rules for the validator as follows:
To verify there are no rows in the table if "editRoad_ProjectClassification" is equal to "NO TREATMENTS".
g$.validator.addMethod("roadWithNoTreatmentsHasNoSegments", function (value, element, params) { var pc = g$('#editRoad_ProjectClassification').val(); var segmentCount = g$('#dataExistingSegments tbody tr').length; if (pc == 'NO TREATMENTS' && segmentCount > 0) { return this.optional(element) || false; } else { return this.optional(element) || true; } }, g$.format("Must have no segments if Project Classification is 'NO TREATMENTS'") );To verify that there is at least one row in the table if "editRoad_ProjectClassification" is not equal to "NO TREATMENTS"
g$.validator.addMethod("roadWithTreatmentsHasAtLeastOneSegment", function (value, element, params) { var pc = g$('#editRoad_ProjectClassification').val(); var segmentCount = g$('#dataExistingSegments tbody tr').length; if (pc != 'NO TREATMENTS' && pc != '' && segmentCount == 0) { return this.optional(element) || false; } else { return this.optional(element) || true; } }, g$.format("Must have at least one segment if Project Classification is not 'NO TREATMENTS'") );Then to validate the form, I have the following:
g$("#roadForm").validate({ errorElement: "span", rules: { editRoad_Jurisdiction: "required", editRoad_TreatmentDate: { "required": true, date: true, roadDateIsValidForMultiYearPlan: true }, editRoad_ProjectClassification: "required", editRoad_ImprovementType: "required", editRoad_SurfaceType: "required", editRoad_MultiYear: "required", editRoad_LifeExpectancy: { "required": true, digits: true }, editRoad_MDOTJobID: { digits: true }, editRoad_ProjectID: { maxlength: 50 }, dataExistingSegments: { "required": true, roadWithTreatmentsHasAtLeastOneSegment: true, roadWithNoTreatmentsHasNoSegments: true } }, messages: { editRoad_Jurisdiction: "Please select an option", editRoad_TreatmentDate: { required: "Please select an date", date: "Please enter a properly formatted date" }, editRoad_ProjectClassification: "Please select an option", editRoad_ImprovementType: "Please select an option", editRoad_SurfaceType: "Please select an option", editRoad_MultiYear: "Please select an option", editRoad_LifeExpectancy: { required: "Please enter a value", digits: "Must be a valid number" }, editRoad_MDOTJobID: { digits: "Must be a valid number" }, editRoad_ProjectID: { maxlength: "Cannot be more than 50 characters long" } } });
This is not working, and I assume it is because the "dataExistingSegments" table is not located within the form. But due to the way the markup and css is done, I can't place this table within the form. How can I get this to work?