34

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.

Peanut
  • 18,967
  • 20
  • 72
  • 78

6 Answers6

52

Use this snippet:

function doSomething()
{
  var myVal = document.getElementById('myValidatorClientID');
  ValidatorEnable(myVal, false); 
}
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • Heads-up: this will not work for ASP.NET 1.1 with Firefox http://stackoverflow.com/questions/3640183/disable-asp-net-1-1-validator-using-javascript-not-working-in-firefox – IrishChieftain Sep 07 '10 at 21:37
  • 15
    Another gotcha is that the validator is still enabled server-side, which may give you a surprise next postback. – JumpingJezza Jun 07 '11 at 08:55
  • 6
    This triggers client-side validation. In the snippet, instead of ValidatorEnable, use: myVal.enabled = false; to disable the validator – GlennG Apr 11 '12 at 15:27
4

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.

Ivan Santiago
  • 1,682
  • 1
  • 17
  • 13
TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
1

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.

Tony L.
  • 17,638
  • 8
  • 69
  • 66
  • 1
    I found this solution to work better for me in framework 4.6.1, the ValidatorEnable gives me a Page.Validators error. – yougotiger Apr 26 '18 at 19:13
  • 1
    I also found that if I had the validators enabled in code and disabled them via JavaScript, they would still validate to true on the server. I disabled them in the front end code, and created a JavaScript function that would enable the validator if required and ran it in Sys.Application.add_load. The validator would client validate and then process server. If it would fail, it would fail on client validate and that was sufficient for me. – yougotiger Jul 22 '20 at 22:56
1

The best way to do like this,

function doSomething()
{
    var objvalidator = document.getElementById('myValidatorClientID');
    objvalidator.enabled = false;
    objvalidator.isvalid = true;
    ValidatorUpdateDisplay(objvalidator);
}
Karthikeyan P
  • 1,216
  • 1
  • 20
  • 23
0

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

Roboblob
  • 1,795
  • 1
  • 22
  • 27
0

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.

Mike Fielden
  • 10,055
  • 14
  • 59
  • 99