Specifying Validation Groups

Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page.

You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group.

During postback, the Page class's IsValid property is set based only on the validation controls in the current validation group. The current validation group is determined by the control that caused validation to occur. For example, if a button control with a validation group of LoginForm is clicked, then the IsValid property will return true if all validation controls whose ValidationGroup property is set to LoginForm are valid. Other controls such as a DropDownList control can also trigger validation if the control's CausesValidation property is set to true (and the AutoPostBack property is set to true.)

To validate programmatically, you can call the Validate method overload that takes the validationGroup parameter to force validation for only that validation group. Note that when you call the Validate method, the IsValid property reflects the validity of all groups validated so far. This could include both a group that was validated as a result of a postback, and the programmatically validated group. If any control in either group is invalid, the IsValid property returns false.

The following code example demonstrates how to use the ValidationGroup property to specify the controls to validate when a Button control posts back to the server. The page contains three text boxes to capture data from the user and three RequiredFieldValidator controls to ensure that the user does not leave a text box blank. The RequiredFieldValidator controls for the first two text boxes are in the PersonalInfoGroup validation group and the RequiredFieldValidator control for the third text box is in the LocationInfoGroup validation group. When Button1 is clicked, only the controls in validation group PersonalInfoGroup are validated. When Button2 is clicked, only the control in validation group LocationInfoGroup is validated.

<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Button.ValidationGroup Example</title>
</head>
<body>
  <form id="form1" runat="server">

    <h3>Button.ValidationGroup Example</h3>

    <asp:label id="NameLabel" 
      text="Enter your name:"
      runat="Server"
      AssociatedControlID="NameTextBox">
    </asp:label>

    &nbsp;

    <asp:textbox id="NameTextBox" 
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator1"
      controltovalidate="NameTextBox"
      validationgroup="PersonalInfoGroup"
      errormessage="Enter your name."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <asp:label id="AgeLabel" 
      text="Enter your age:"
      runat="Server"
      AssociatedControlID="AgeTextbox">
    </asp:label>

    &nbsp;

    <asp:textbox id="AgeTextbox" 
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator2"
      controltovalidate="AgeTextBox"
      validationgroup="PersonalInfoGroup"
      errormessage="Enter your age."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <!--When Button1 is clicked, only validation
    controls that are a part of PersonalInfoGroup
    are validated.-->
    <asp:button id="Button1" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="PersonalInfoGroup"
      runat="Server" />

    <br /><br />

    <asp:label id="CityLabel" 
      text="Enter your city of residence:"
      runat="Server"
       AssociatedControlID="CityTextbox">
    </asp:label>

    &nbsp;

    <asp:textbox id="CityTextbox" 
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator3"
      controltovalidate="CityTextBox"
      validationgroup="LocationInfoGroup"
      errormessage="Enter a city name."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <!--When Button2 is clicked, only validation
    controls that are a part of LocationInfoGroup
    are validated.-->
    <asp:button id="Button2" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="LocationInfoGroup"
      runat="Server" />

  </form>
</body>
</html>
<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>Button.ValidationGroup Example</title>
</head>
<body>
  <form id="form1" runat="server">

    <h3>Button.ValidationGroup Example</h3>

    <asp:label id="NameLabel" 
      text="Enter your name:"
      runat="Server"
      AssociatedControlID="NameTextBox">
    </asp:label>

    &nbsp;

    <asp:textbox id="NameTextBox" 
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator1"
      controltovalidate="NameTextBox"
      validationgroup="PersonalInfoGroup"
      errormessage="Enter your name."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <asp:label id="AgeLabel" 
      text="Enter your age:"
      runat="Server" 
      AssociatedControlID="AgeTextBox">
    </asp:label>

    &nbsp;

    <asp:textbox id="AgeTextBox" 
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator2"
      controltovalidate="AgeTextBox"
      validationgroup="PersonalInfoGroup"
      errormessage="Enter your age."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <!--When Button1 is clicked, only validation
    controls that are a part of PersonalInfoGroup
    are validated.-->
    <asp:button id="Button1" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="PersonalInfoGroup"
      runat="Server" />

    <br /><br />

    <asp:label id="CityLabel" 
      text="Enter your city of residence:"
      runat="Server" 
      AssociatedControlID="CityTextBox">
    </asp:label>

    &nbsp;

    <asp:textbox id="CityTextBox" 
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator3"
      controltovalidate="CityTextBox"
      validationgroup="LocationInfoGroup"
      errormessage="Enter a city name."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <!--When Button2 is clicked, only validation
    controls that are a part of LocationInfoGroup
    are validated.-->
    <asp:button id="Button2" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="LocationInfoGroup"
      runat="Server" />

  </form>
</body>
</html>

See Also

Other Resources

ASP.NET Validation Controls