13

I have a complex form requiring me to switch specific validators on or off depending on selections made by the user.

ValidatorEnable seems to do the job, but it seems that when I call this method it actually fires the validation process as well, without the user actually hitting the submit button.

Is that how it works?

0

2 Answers 2

22

I eventually found the best way to do this was to use the following code:

var validatorObject = document.getElementById('<%=ValidHasShippingLocationZip.ClientID%>');
validatorObject.enabled = false;
validatorObject.isvalid = true;
ValidatorUpdateDisplay(validatorObject);
4
  • 1
    it worked, I didn't use the isvalid property, I worked just with the enable or disable Apr 26, 2013 at 20:04
  • Why I'm still able to submit my asp.net form while the above function works with my validators and display / hide the validator? I don't use isValid in my code behind!
    – hsobhy
    Mar 14, 2016 at 16:02
  • @DKR make sure you find correct validatorObject, maybe you should use console.log(validatorObject) to see if the element was find correctly. Jan 23, 2017 at 6:44
  • It worked, but has 1 problem, Page.Isvalid still returns false (I mean on code-behind side) Jan 23, 2017 at 6:50
1

I wrote some code seems can meet your requests. Iterate validators and enable these you needs.

   ValidatorEnable(validatorObj, true);

then clear the screen,erase the error info.

The full code snippet can be found here http://codelife.cybtamin.com/enable-and-disable-asp-net-validator-by-javascript/

2
  • 1
    Don't just post links, links die.
    – tjleigh
    Oct 19, 2016 at 13:56
  • ValidatorEnable(valObj, enable) is now part of the .NET boilerplate code so this example can be used without additional code. Nov 28, 2016 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.