Disable requiredfieldvalidator with javascript

10,578

Solution 1

Have you tried using..

    ValidatorEnable(document.getElementById('<%= rfvFirstname.ClientID %>'), false);

Solution 2

We can able to do with jquery like this

$("#ObjectID").removeAttr("data-val-required");
Share:
10,578
nick gowdy
Author by

nick gowdy

Updated on June 14, 2022

Comments

  • nick gowdy
    nick gowdy almost 2 years

    Firstly I have seen the other threads on this topic and I just want something cleared up that I am stuck on.

    I have this textbox control and required field validator:

    <td class="style1">
        <asp:TextBox runat="server" ID="txtFirstname"></asp:TextBox>
    </td>
    <td>
        <asp:RequiredFieldValidator ID="rfvFirstname" runat="server"   ControlToValidate="txtFirstname" >*</asp:RequiredFieldValidator>
    </td>
    

    And this javascript code

    <script type="text/javascript">
        function chkValidators() {
            alert("enter function chkValidators");
            validatorEnable(document.getElementById('<%rfvFirstname%>'), false);
        }
    

    This is giving me a compilation error:

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
    
    Compiler Error Message: CS1002: ; expected
    
    Source Error:
    
    Line 158:            #line default
    Line 159:            #line hidden
    Line 160:            @__w.Write("\'), false);\r\n         }\r\n   </script>\r\n");
    Line 161:        }
    Line 162:        
    

    Anyone know how to fix this?

    Nick

  • Sid M
    Sid M about 10 years
    in ClientID 'd' must be capital.
  • jtate
    jtate over 9 years
    validatorEnable should be ValidatorEnable (case-sensitive)
  • smoothumut
    smoothumut almost 9 years
    make sure EnableClientScript is true