Disable ASP.NET validators with JavaScript

后端 未结 6 1229
小鲜肉
小鲜肉 2020-12-05 04:23

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

相关标签:
6条回答
  • 2020-12-05 05:00

    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.

    0 讨论(0)
  • 2020-12-05 05:07

    Use this snippet:

    function doSomething()
    {
      var myVal = document.getElementById('myValidatorClientID');
      ValidatorEnable(myVal, false); 
    }
    
    0 讨论(0)
  • 2020-12-05 05:07

    The best way to do like this,

    function doSomething()
    {
        var objvalidator = document.getElementById('myValidatorClientID');
        objvalidator.enabled = false;
        objvalidator.isvalid = true;
        ValidatorUpdateDisplay(objvalidator);
    }
    
    0 讨论(0)
  • 2020-12-05 05:08

    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.

    0 讨论(0)
  • 2020-12-05 05:10

    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.

    0 讨论(0)
  • 2020-12-05 05:17

    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

    0 讨论(0)
提交回复
热议问题