I am working on an ASP MVC3 project, using validate and unobtrusive jquery. I have several area on a page that I need to create custom validation for, and ensure it doesn't break anything else. Currently I have several sections, each have a radio button pair, some text boxes, a button and a table. User fills in the text boxes, hits the button, and I have a function that puts the data from the textboxes into a new row on the table. I also have a counter that is advanced once with each button press.
The validation needs to be along the lines of "If the radio button returned true, the counter needs to be > 1.
I wrote this function that seems like it would work, but I believe I need to re-write it so it is tied into the existing validation, hence the need to add a method or rule:
- $(document).ready(function () {
- $("#nextBtn").click(function () {
- var rows = $("#ROTable tr").length;
- if ($("#RO_Yes").is("checked") && (rows < 3))
- {
- $(this).closest("div.historyCollection").css("backgroundcolor: #ff0000;");
- }
- });
- });
I have looked around and I found a section in this site discussing Rules (dependant value) which seems like it may fit. Butbecause I am pretty new to this stuff, I wanted to poll the experts before I look too hard in one direction. Thank you for reading.
EDIT: I have been playing around and I came up with this:
- $(document).ready(function () {
- $("ROCounter").rules("add", {
- required: $("#RO-Yes").is(":checked"),
- messages: {
- required: "Please enter further information"
- }
- });
- });
but it broke all the validation and caused an error in the jquery.validate.min.js file:
" TypeError: Cannot read property 'form' of undefined"