How to disable asp.net validator on client side?

12,053

Solution 1

You would have to programmably disable it when the page posts back, however you interpret that validator should be disabled on the client then would need to be replicated on the server, and set Enabled="false".

Solution 2

You should use EnableClientScript="False" on the validation control, that is:

<asp:RequiredFieldValidator ID="rfvText" 
      runat="server" ControlToValidate="tbText"
      ValidationGroup="Address" 
      EnableClientScript="False"
      ErrorMessage="RequiredFieldValidator">Enter Text
 </asp:RequiredFieldValidator>
Share:
12,053
ihorko
Author by

ihorko

Updated on June 05, 2022

Comments

  • ihorko
    ihorko almost 2 years

    I have very simple aspx page:

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:textbox runat="server" ID="tbText" ValidationGroup="Address"></asp:textbox>
            <br />
            <asp:RequiredFieldValidator ID="rfvText" runat="server" ControlToValidate="tbText" 
                ValidationGroup="Address" ErrorMessage="RequiredFieldValidator">Enter text</asp:RequiredFieldValidator>
            <br />
            <asp:Button runat="server" Text="Submit" ID="btnSubmit" OnClick="Submit_Click" 
                ValidationGroup="Address" OnClientClick="DisableValidator();" />
            <script type="text/javascript">
                function DisableValidator() {
                    alert('Called and disable validators before submit');
    
                    var validator = document.getElementById("<%=rfvText.ClientID%>");
                    validator.validationGroup = "someGroup";
                    ValidatorEnable(validator, false);
                }
            </script>
        </div>
        </form>
    </body>
    </html>
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void Submit_Click(object sender, EventArgs e)
        {
            Validate("Address");
            if (!IsValid)
            {
                throw new Exception("Page is no valid");
            }
        }
    }
    

    All elements on the page have ValidationGroup="Address", but I need disable my validator just before I click on the button. So, on client site it disables, but when I try to validate it on the server, my page isn't valid on the server but valid on client.

    How can I disable validator on client to became disable on the server also?

    Thanks!

  • Jeff
    Jeff almost 12 years
    What version .NET are you running?
  • Jeff
    Jeff almost 12 years
    Doh. That property is new to .NET 4.