Disable ASP.NET validators with JavaScript

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use this snippet:

function doSomething()
{
  var myVal = document.getElementById('myValidatorClientID');
  ValidatorEnable(myVal, false); 
}

Method 2

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

  1. Make sure you can set a break point and can single step through the code. If not restart browser.
  2. 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.
  3. 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.

Method 3

The best way to do like this,

function doSomething()
{
    var objvalidator = document.getElementById('myValidatorClientID');
    objvalidator.enabled = false;
    objvalidator.isvalid = true;
    ValidatorUpdateDisplay(objvalidator);
}

Method 4

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.

Method 5

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.

Method 6

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x