articles

Home / DeveloperSection / Articles / Validation Group Property in Asp.Net

Validation Group Property in Asp.Net

Anonymous User19207 09-Jul-2012

ASP.Net 2.0 supports validation groups, which is a new feature that is included in this version. The validation groups help you to group the controls in a single page and you can have separate submit buttons for each group, so that you can submit each group individually. Each group can have separate validation controls.

When you submit a group of values from a group, the validation controls pertaining to that group alone is executed. Submission of a particular group of controls will not execute the validation controls of another group. This helps you to group your controls and have separate validation controls for each group.

Consider a scenario where there is a web form containing groups of textbox control. Each group of text box control there gets input values like User Email Id and User Password (In case of existing member) or User Email and Mobile No (In case of Guest User).  These two sections work individually that means each group submitted server side separately and each of them can have its own validation controls.  Let us say that you have Required Field Validation for all the control of each group. If you do not enter any value in text box control Required Field Validation Control will fired, even if you do not enter value in other group yet it fired too. Let’s have a brief demonstration on it.

In this demonstration, I have created two group controls: Existing Member and Guest User and both are having Email Validation and Required Field Validation.

Validation Group Property in Asp.Net

Now, Here User can login either with Existing Member Credential or Guest User Credential. So when I fill all information for Existing User Group and when clicked on button ‘Login’ then it does not posted on server side because of Guest Validation Group has been fired but I want to perform it separately.

Validation Group Property in Asp.Net

So To resolve this problem we have to use Validation Group Property. Here I have created grpExistingMember  (for Existing Member Login) and grpGuestUser (For Guest User). Now, login with any type user either existing member login or guest user login.

ASPX Code:
<%@ 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>
        <table >
            <tr>
                <td>
                    <table style="border: 1px solid Teal; ">
                        <tr>
                            <th style="background-color: Teal; color: White;">
                                ::Member Login::
                            </th>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblEmailID" runat="server" Text="Email Id"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                                    ControlToValidate="txtEmailId" ErrorMessage="*"
                                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                                    ValidationGroup="grpExistingMember"></asp:RegularExpressionValidator>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                                    ControlToValidate="txtEmailId" ErrorMessage="*"                                   ValidationGroup="grpExistingMember"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                                    ControlToValidate="txtPassword" ErrorMessage="*"
                                    ValidationGroup="grpExistingMember"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td>
                                <asp:Button ID="btnSubmit" runat="server" Text="Login"
                                    ValidationGroup="grpExistingMember" />
                            </td>
                        </tr>
                    </table>
                </td>
                <td>
                    <table style="border: 1px solid Teal; ">
                        <tr>
                            <th style="background-color: Teal; color: White; '">
                                ::Guest User Login::
                            </th>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblGEmail" runat="server" Text="Email Id"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txtGEmail" runat="server"></asp:TextBox>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
                                    ControlToValidate="txtGEmail" ErrorMessage="*"
                                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                                    ValidationGroup="grpGuestUser"></asp:RegularExpressionValidator>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                                    ControlToValidate="txtGEmail" ErrorMessage="*"
                                    ValidationGroup="grpGuestUser"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblGPassword" runat="server" Text="Mobile No."></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txtGPassword" runat="server" TextMode="Password" Width="128px"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
                                    ControlToValidate="txtGPassword" ErrorMessage="*"
                                    ValidationGroup="grpGuestUser"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td>
                                <asp:Button ID="btnGSubmit" runat="server" Text="Login"
                                    ValidationGroup="grpGuestUser" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html> 

Validation Group Property in Asp.Net

Now click on button ‘Login’.

Validation Group Property in Asp.Net


Updated 06-May-2020
I am a content writter !

Leave Comment

Comments

Liked By