Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
Peanut Avatar asked Dec 30 '08 14:12

Peanut


People also ask

How do I disable a validator?

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 is Validatorenable in Javascript?

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.

What is validator control advantages?

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.


2 Answers

Use this snippet:

function doSomething() {   var myVal = document.getElementById('myValidatorClientID');   ValidatorEnable(myVal, false);  } 
like image 74
Andreas Grech Avatar answered Oct 05 '22 16:10

Andreas Grech


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.

like image 35
Hammad Khan Avatar answered Oct 05 '22 16:10

Hammad Khan