Web Tutorials

Interview Q & A

Code Examples

Utility Tools

ASP.NET Confirm Password using CompareValidator

ASP.NET CompareValidator is very useful when need to check two string are equal or not. CompareValidator is useful in compare in confirm password validation. The CompareValidator control belongs to System.Web.UI.WebControls

Syntax

<asp:CompareValidator ID="CompareValidator1" runat="server" 
            ControlToCompare="TextBox2" ControlToValidate="TextBox3" 
            ErrorMessage="Password does not match."></asp:CompareValidator>

ASP.NET CompareValidator 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.
EqualThis is used to check whether the compared value are equal.
NotEqualThis is used to check whether the compared value are not equal.
GreaterThanThis is used to check whether the compared value are greater than.
GreaterThanEqualThis is used to check whether the compared value are greater than equal
LessThanThis is used to check whether the compared value are less than.
LessThanEqualThis is used to check whether the compared value are less than equal.
DataTypeCheckThis is used to compare data type.

ASP.NET Confirm Password using CompareValidator 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 CompareValidator Example</title>
</head>
<body>
<form id="Form" runat="server">
<div>
<asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="Medium" 
	Font-Underline="True" Text="CompareValidator Control Example"></asp:Label>
<br />
<asp:Label ID="lblName" runat="server" Font-Bold="True" Font-Size="Small" 
	Text="User Name:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="173px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
	ControlToValidate="TextBox1" ErrorMessage="This Field can not be blank."></asp:RequiredFieldValidator>
<br />
<asp:Label ID="lblPassword" runat="server" Font-Bold="True" Font-Size="Small" 
	Text="Password:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="173px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
	ControlToValidate="TextBox2" ErrorMessage="This Field can not be blank."></asp:RequiredFieldValidator>
<br />
<asp:Label ID="lblRePassword" runat="server" Font-Bold="True" Font-Size="Small" 
Text="Re-type Password:"></asp:Label>
 <asp:TextBox ID="TextBox3" runat="server" TextMode="Password" Width="173px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" 
	ControlToCompare="TextBox2" ControlToValidate="TextBox3" 
	ErrorMessage="Password does not match."></asp:CompareValidator>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
	ControlToValidate="TextBox3" ErrorMessage="This Field can not be blank."></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btnSubmit" runat="server" Font-Bold="True" 
	Font-Size="Small" Text="Submit" />
<br />
</div>
</form>
</body>
</html>

Above example will produce following output

RequiredFieldValidator

RegularExpressionValidator

RangValidator

CompareValidator

CustomValidator

ValidationSummary