I recently had to add functionality to a ASP.Net 3.5 web form of ours that allowed a user to provide a suggested alternative when the value they needed wasn’t available in a drop down box. To do this, I wanted to be able to turn our client side validation on and off for the controls involved.
One of the things we did early on in this project was to create a set of user controls which wrap labeling, watermarking, validation and some other features to the usual set of ASP.Net form controls. This simplifies our mark-up considerably by encapsulating several server controls into one user control that’s configurable through attributes. All these super controls also have client-side validation enabled (and in some cases, custom implemented) so that it’s nearly trivial to develop a solid form view/list view rather quickly.
Since we’re using as much out-of-the-box functionality as we can for the client side validation, I didn’t want to have to go outside of our normal pattern to provide the desired functionality. So when the user was going with an value in the drop down, I needed the client-side validation for the suggestion text box turned off, but if the user selected the “Other” option in the drop down, I’d reveal the suggestion text box and turn on the client side validation… this took me a while to figure out and I didn’t really find much in the way of documentation to help me. (perhaps there’s a better way? if so, let me know)
Since I wasn’t finding much in the way of help, I simply started digging through the javascript sent to the browser for the form. What I found (after lots of digging) is that the standard validation controls in ASP.Net get added to an array during page initialization.
var validatorControl = document.getElementById("validationControlClientId");
// some initialization happens over several lines
Array.add(Page_Validators, validatorControl);
The take-away being that there’s an array of the validator controls which the validation script uses to run through to evaluate all the client side validation pre-submit. Since we’re generating all these controls as part of a larger user control, I knew the naming convention that was used for the validation controls.
All I had to do then was:
- Get the client id for the control I wanted to toggle validation for
- Formulate the client id for the validation control
- Add/Remove the validation control from the Page_Validators array
Now, I know a lot of people get very particular about how you select elements from HTML and that you don’t want it to be so brittle that anyone making harmless changes to the page later could break your jQuery. What I did was to add superficial css classes to the container for the super control so I could get the client id, then I wrote a simple function that would turn client-side validation on or off for that type of control.
var suggestedText = $("input.Suggestion");
function ToggleTextBoxValidation(control, enabled)
{
// the rqf prefix is for Required Field
var id = control.attr("id").replace("textbox", "rqfText")
// gets the actual ASP.Net validator control
var validator = document.getElementById(id);
if (enabled) {
Array.add(Page_Validators, validator);
}
else {
Array.remove(Page_Validators, validator);
}
}
So this is obviously a fairly watered down version from what I have in my page but hopefully this illustrates clearly enough how you can turn ASP.Net client side validation on or off using jQuery.
Tags: jquery, validation, asp.net