<asp:CompareValidator
id="ProgrammaticID"
ControlToValidate="Programmatic ID of Server Control to Validate"
ValueToCompare="value"
ControlToCompare="value"
Type="DataType"
Operator="Operator Value"
ErrorMessage="Message to display in ValidationSummary control"
Text="Message to display in control"
ForeColor="value"
BackColor="value"
runat="server" >
</asp:CompareValidator>
The CompareValidator control allows you to compare the value entered by the user into an input control, such as a TextBox control, with the value entered into another input control, or with a constant value. You can also use the CompareValidator control to determine whether the value entered into an input control can be converted to the data type specified by the Type property.
Specify the input control to validate by setting the ControlToValidate property. If you want to compare a specific input control with another input control, set the ControlToCompare property with the name of the control to compare.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CompareValidator.aspx.cs" Inherits="CompareValidator" %>
<!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>ASP.Net CompareValidator Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">CompareValidator</h2>
<asp:Label ID="lbl1Example" runat="server">
</asp:Label>
<br /><br />
<asp:Label ID="lbl2Example" runat="server">
</asp:Label>
<asp:TextBox ID="txtExample" runat="server">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidatorExample" runat="server">
</asp:CompareValidator>
<br />
<asp:Label ID="lbl3Example" runat="server">
</asp:Label>
<asp:TextBox ID="txt2Example" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorExample"
runat="server"
ControlToValidate="txt2Example"
Text="*"
>
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="ButtonExample" runat="server" Text="Submit Password" OnClick="ButtonExample_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class CompareValidator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
lbl1Example.ForeColor = System.Drawing.Color.Green;
lbl1Example.Font.Italic = true;
lbl2Example.Text = "Password";
lbl3Example.Text = "Re-Type Password";
txtExample.TextMode = TextBoxMode.Password;
txt2Example.TextMode = TextBoxMode.Password;
ButtonExample.Font.Bold = true;
ButtonExample.ForeColor = System.Drawing.Color.Green;
CompareValidatorExample.ControlToValidate = "txt2Example";
CompareValidatorExample.ControlToCompare = "txtExample";
CompareValidatorExample.ErrorMessage = "Password do not match!";
}
}
protected void ButtonExample_Click(object sender, EventArgs e) {
lbl1Example.Text = "Password match.";
}
}
ASP.Net CompareValidator Example, How to <asp:CompareValidator> Control compare the value in asp.net c#
Hot on Web: