UPDATE:
I've come up with a solution to this which I've written as the accepted answer below.
Another option is to apply validation rules to all elements initially, add an ".invalid
" class to elements you don't want to validate. Swap this class in & out of elements you want to validate as needed.
I have a form that toggles what input elements are visible. I want to validate only the visible elements on the form.
I'm having a hard time getting this to function correctly. I want to disable validators for non-visible elements and I can't seem to figure out the best way to do this. Any insight to what may be wrong with my code, or my approach would be appreciated.
When visibility is toggled, I've tried a few things:
- Calling
$("form").rules("remove")
to clear all existing validation rules. This throws an "undefined" JavaScript exception. - Calling
$("form").validation(...options...)
for the visible elements, hoping this would overwrite the rules. This only allows the first group that is validated to work. The second panel can not be validated. - Unbinding the submit handler before calling the new
validation()
method. This didn't do what I would have thought. It removes all validation (seemingly) permanently and the form submits without validation. - Clearing out the validation object with
$.removeData($('form'), 'validator')
before trying to call the validator again. This also doesn't work. - This is in an ASP.NET site, so using multiple
<form />
tags is out of the question since that would break the page.
I'm sort of stumped on how I can make this work. You can see a complete working demo of what I have at http://jsbin.com/ucibe3
, or edit it at http://jsbin.com/ucibe3/edit
. I've tried to strip it down to just the code that causes the bug.
Here are the key pieces of my code (use above links for complete working page)
HTML:
<td id="leftform">
Left Form<br />
Input 1: <input type="text" name="leftform_input1" /><br />
Input 2: <input type="text" name="leftform_input2" /><br />
<input type="submit" name="leftform_submit" value="Submit Left" />
</td>
<td id="rightform" style="visibility:hidden">
Right Form<br />
Input 1: <input type="text" name="rightform_input1" /><br />
Input 2: <input type="text" name="rightform_input2" /><br />
<input type="submit" name="rightform_submit" value="Submit Right" />
</td>
JavaScript:
$('#toggleSwitch').click(function() {
if ($('#leftform').css("visibility") !== "hidden") {
$('#leftform').css("visibility", "hidden");
$('#rightform').css("visibility", "visible");
$('form').validate({
rules: {
rightform_input1: { required: true },
rightform_input2: { required: true }
},
messages: {
rightform_input1: "Field is required",
rightform_input2: "Field is required"
}
});
} else {
$('#rightform').css("visibility", "hidden");
$('#leftform').css("visibility", "visible");
$('form').validate({
rules: {
leftform_input1: { required: true },
leftform_input2: { required: true }
},
messages: {
leftform_input1: "Field is required",
leftform_input2: "Field is required"
}
});
}
});