Web Tutorials

Interview Q & A

Code Examples

Utility Tools

ASP.NET Grouping using RequiredFieldValidator

ASP.NET grouping is implemented by setting ValidationGroup property of RequiredFieldValidator to ensure that the user has entered the data into input control. The RequireField control belongs to System.Web.UI.WebControls

Syntax

<asp:RequiredFieldValidator id="reqUserName"
ControlToValidate="txtUserName" Text="(Required)"
ValidationGroup="LoginGroup" Runat="server" />

ASP.NET RequireFieldValidator Control Property

PropertyDescription
ControlToValidateSpecifies control name which needs to be validate.
DisplaySpecifies behavior of the error message.
EnableClientScriptSpecifies whether or not client-side validation is enabled.
EnabledSpecifies whether or not validation control is enabled or not.
ErrorMessageSpecifies error messageto be display in validation summary.
ForeColorSpecifies error message color.
IsValidIndicate if associate control passes validation or not.
SetFocusOnErrorSet focus on associate control if validation fails.
TextSpecifies text to be display if validation fails.
ValidationGroupSpecifies validation group name.
ValidateUpdate the isvalid propery.
ValidateGetValidationPropertyDetermineing validation property of a control, if it exists.
InitialValueSpecifies initial value of input control.

ASP.NET Grouping using RequiredFieldValidator Example

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" 
Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET RequiredFieldValidator Group Example</title>
</head>
<body>
<form id="Form" runat="server">  
<div style="width: 619px">  
<asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="Medium"   
Font-Underline="True" Text="ASP.NET RequiredFieldValidator Grouping Example"></asp:Label><br /><br /> 
<fieldset>
<table border="0" width="600px" cellpadding="2" cellspacing="0">  
<tr><td>  
<asp:Label id="lblUserName" Text="Employee G1 Name:"
AssociatedControlID="txtUserName" Runat="server" />
          
<asp:TextBox id="txtUserName" Runat="server" />  
<asp:RequiredFieldValidator id="reqUserName"
ControlToValidate="txtUserName" Text="(Required)"
ValidationGroup="LoginGroup" Runat="server" />  
</td></tr> 

<tr><td>  
<asp:Label id="lblPassword" Text="Employee G1 Password:"
AssociatedControlID="txtPassword" Runat="server" />
          
<asp:TextBox id="txtPassword" TextMode="Password"
Runat="server" /> 
<asp:RequiredFieldValidator id="reqPassword"
ControlToValidate="txtPassword" Text="(Required)"
ValidationGroup="LoginGroup" Runat="server" />  
</td></tr> 
<tr><td>
<asp:Button id="btnLogin" Text="G1 Login"
ValidationGroup="LoginGroup" Runat="server" />
</td></tr> 

<tr><td>  
<asp:Label id="lblFirstName"
Text="Employee G2 Name:" AssociatedControlID="txtFirstName"
Runat="server" />
          
<asp:TextBox id="txtFirstName" Runat="server" />  
<asp:RequiredFieldValidator id="reqFirstName"
ControlToValidate="txtFirstName" Text="(Required)"
ValidationGroup="RegisterGroup" Runat="server" /> 
</td></tr> 

<tr><td>  
<asp:Label id="lblEmpG2" Text="Employee G2 Password::"
AssociatedControlID="txtPassword" Runat="server" />
          
<asp:TextBox id="txtPassG2" Runat="server" TextMode="Password"/>
<asp:RequiredFieldValidator id="reqLastName"
ControlToValidate="txtPassG2" Text="(Required)"
ValidationGroup="RegisterGroup" Runat="server" />  
</td></tr> 
<tr><td>
<asp:Button id="btnRegister"
Text="G2 Login" ValidationGroup="RegisterGroup"
Runat="server" />
</td></tr> 

</table>  
</fieldset> 
</div>   
</form>
</html>

Above example will produce following output

RequiredFieldValidator

RegularExpressionValidator

RangValidator

CompareValidator

CustomValidator

ValidationSummary