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
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.
Use this snippet:
function doSomething()
{
var myVal = document.getElementById('myValidatorClientID');
ValidatorEnable(myVal, false);
}
The best way to do like this,
function doSomething()
{
var objvalidator = document.getElementById('myValidatorClientID');
objvalidator.enabled = false;
objvalidator.isvalid = true;
ValidatorUpdateDisplay(objvalidator);
}
Per Glenn G's comment above, I would use
myVal.enabled = false;
to disable the validator instead of
ValidatorEnable(myVal, false);
Here's what I experienced: Both snippets worked fine on my 2003 server with .net framework 2.0. However, on server 2008 R2 with .net framework 4.0 (I'm not sure which is causing the issue), I was getting unintended behavior with "ValidatorEnable". There are tabs (usercontrols) on my page and when I would leave the tab that was calling "ValidatorEnable", it was temporarily disabling validators on the next tab (even though code was not being called to disable them) until the next postback.
Additionally, rather than simply hiding elements you could set the Visible property to false...
whateverItem.Visible = false;
This makes that item simply not render to the page, which I believe disables the validation. Someone please correct me if I am wrong.
Here is detailed article on how to control ASP.NET Validators from JavaScript:
How to control ASP.NET Validator Controls Client Side validation from JavaScript