ValidationGroup example in Asp.Net

In this post, we will discuss how to use ValidationGroup in Asp.Net.

You can also check my previous posts on ValidationSummaryRequiredFieldValidator, RangeValidator, RegularExpressionValidator examples in Asp.Net.

If your page has several groups of controls and you want to perform validation separately then ValidationGroup will be very much helpful.

You have to assign ValidationGroup to controls, validators, and buttons. Here in this example, we have two panels.

Below is the .aspx code:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”ValidatorExamples.aspx.cs”
Inherits=”ValidatorExamples” %>

<!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>ValidationGroup example in Asp.Net</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Panel ID=”pnlLogin” runat=”server”>
Enter EmailID:<asp:TextBox ID=”txtEmailIDLogin” runat=”server” ValidationGroup=”Group1″></asp:TextBox><br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server” ForeColor=”Red”
ValidationGroup=”Group1″ ErrorMessage=”Enter Email ID!” ControlToValidate=”txtEmailIDLogin”></asp:RequiredFieldValidator><br />
<asp:Button ID=”btnLogin” runat=”server” Text=”Submit” ValidationGroup=”Group1″ />
</asp:Panel>
<asp:Panel ID=”pnlSignup” runat=”server”>
Enter EmailID: <asp:TextBox ID=”txtEmailIDSignUp” runat=”server” ValidationGroup=”Group2″> </asp:TextBox>
<br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator2″ runat=”server” ForeColor=”Red” ErrorMessage=”Enter Email ID!” ControlToValidate=”txtEmailIDSignUp” ValidationGroup=”Group2″></asp:RequiredFieldValidator>
<br />
<asp:Button ID=”btnSignup” runat=”server” Text=”Submit” ValidationGroup=”Group2″/>
</asp:Panel>
</div>
</form>
</body>
</html>


Here if you click on first button then only 1st textbox will get validated. And also if you click on second button then 2nd textbox will get validated like below figure.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply