Disable Validations in ASP.NET by using ValidationGroup and CausesValidation attributes

 

In one of my previous articles I explain about validation controls in Asp.Net. Today we discuss about how to disable the validation for particular button on web farm.

 

For example we have form with two text boxes for user name, password and two buttons for submit and cancel buttons. We have RequiredFieldValidator control for two text boxes to validate the input as shown below.

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

 

    <title>Disable Validation</title>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

        User Name:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>

 

        <asp:RequiredFieldValidator id="reqUserName" ControlToValidate="txtUserName" Text="*Enter User Name" Runat="server" />

 

        <br />

 

        Password:<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>

 

        <asp:RequiredFieldValidator id="reqPassword" ControlToValidate="txtPassword" Text="*Enter Password" Runat="server" />

 

        <br />

 

        <asp:Button ID="btnSubmit" runat="server" Text="Submit"/>

 

        <asp:Button ID="btnCancel" runat="server" Text="Cancel"/>

 

    </div>

 

    </form>

 

</body>

 

</html>

 

You need to validate text boxes when user clicks on Submit button and it works fine. But even if we click on Cancel button also it validates the text boxes which we don’t want. To avoid validation when we click on Cancel button, we have two options. One is apply ValidationGroup attribute for text boxes and submit button and another one is change the CausesValidation attribute value of Cancel button to false.

 

 

Apply ValidationGroup attribute for text boxes and submit button as shown below.

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

 

    <title>Disable Validation using ValidationGroup </title>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

        User Name:<asp:TextBox ID="txtUserName" ValidationGroup="vInput" runat="server"></asp:TextBox>

 

        <asp:RequiredFieldValidator id="reqUserName" ValidationGroup="vInput" ControlToValidate="txtUserName" Text="*Enter User Name" Runat="server" />

 

        <br />

 

        Password:<asp:TextBox ID="txtPassword" ValidationGroup="vInput" runat="server"></asp:TextBox>

 

        <asp:RequiredFieldValidator id="reqPassword" ValidationGroup="vInput" ControlToValidate="txtPassword" Text="*Enter Password" Runat="server" />

 

        <br />

 

        <asp:Button ID="btnSubmit" runat="server" ValidationGroup="vInput" Text="Submit"/>

 

        <asp:Button ID="btnCancel" runat="server" Text="Cancel"/>

 

    </div>

 

    </form>

 

</body>

 

</html>

 

As we required validation happen whenever we click on Submit button only. We are restricting the validation for Cancel button functionality by applying the same value(here Vinput”) to the ValidationGroup attribute of text boxes, validation controls and for Submit button.

 

 

 

Change CausesValidation attribute value of Cancel button as shown below

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

 

    <title>Disable Validation using CausesValidation </title>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

        User Name:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>

 

        <asp:RequiredFieldValidator id="reqUserName" ControlToValidate="txtUserName" Text="*Enter User Name" Runat="server" />

 

        <br />

 

        Password:<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>

 

        <asp:RequiredFieldValidator id="reqPassword" ControlToValidate="txtPassword" Text="*Enter Password" Runat="server" />

 

        <br />

 

        <asp:Button ID="btnSubmit" runat="server" Text="Submit"/>

 

        <asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false"/>

 

    </div>

 

    </form>

 

</body>

 

</html>

 

As we required validation happen whenever we click on Submit button only. We are restricting the validation for Cancel button functionality by making CausesValidation attribute value to false.