Does anyone know how to disable an ASP.NET validator using JavaScript? I'm using javascript style.display = 'none' to disable parts of a web page. However these disabled parts have asp.net validators that are still firing and I need to disable then without doing a round trip to the server. Thanks.
To disable individual validators, clear the check boxes next to each validator that you want to disable. Each validator has a check box to specify whether it is enabled for manual validation or on a build.
What it does is: Use a customValidator (not a RequiredFieldValidator) control that validates the textbox using a javascript function called Validator. The validator function checks if the checkbox is checked and validates the textbox.
Advantage - No requirement from the client machine since all execution is done at the server side, so it considered to be safer compared to the client side. Disadvantage - This kind of validation requires a trip to the server and back, so the performance is low compared to client Side.
Use this snippet:
function doSomething() { var myVal = document.getElementById('myValidatorClientID'); ValidatorEnable(myVal, false); }
This is an old question but surprised the following answer it not given. I implemented it using of the comments above and works flawlessly
# enable validation document.getElementById('<%=mytextbox.ClientID%>').enabled = true #disable validation document.getElementById('<%=mytextbox.ClientID%>').enabled = false
This implementation can be someone tricky because remember if there is an error in javascript, the code will exit but you wont get any errors. Some helpful points
Make sure you can set a break point and can single step through the code. If not restart browser.
Set the break point at the start of the function where you put this code. Step through the code, if there is an error, the code will exit and you won't be able to step into the code.
In firefox Firebug, you can type the entire javascript statement in console tab to see what your statement returns. Most importantly you want to check for not null values of controls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With