ASP.NET


3. The CompareValidator tutorials
-
The CompareValidator can be used to compare the value of one control to another value.
-
This is often used in sign-up forms where a user has to enter a password twice to make sure they type the same password both times. Alternatively, instead of comparing to another control, you can also compare against a constant value.
-
The following table lists the additional properties for the CompareValidator control.
Properties |
Description |
ControlToCompare |
This property contains the ID of the control that the validator compares against. When this property is set, ValueToCompare has no effect. |
Operator |
This property determines the type of compare operation. For example, when Operator is set to Equal both controls must contain the same value for the validator to be considered valid. Similarly, you have options like NotEqual, GreaterThan, and GreaterThanEqual to perform different validation operations. |
Type |
This property determines the data type that the validation control Checks. This value can be set to String, Integer, Double, Date, or Currency to check the respective data types. |
ValueToCompare |
This property allows you to define a constant value to compare against. This is often used in agreements where you have to enter a word like Yes to indicate you agree to some condition. Simply set the ValueToCompare to the word Yes and the ControlToValidate to the control you want to validate and you’re done. When this property is set, make sure you clear the ControlToCompare property as that will otherwise take precedence. |
-
Example: In the below example, we have taken two textboxes and button control for compairing the textboxes field using compare validator.
CompareValidator.aspx.cs Code:
|
Following example shows the use of ASP.NET CompareValidator. |
![]() |
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class CompareValidator : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}} |
|
|
CompareValidator.aspx Code:
|
Following example shows the use of ASP.NET CompareValidator. |
![]() |
<%@ 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>cbtSAM ASP.NET Compare Validator</title><style type="text/css">.style1{width: 100%;}</style></head><body><form id="form1" runat="server"><div><table class="style1"><tr><td> </td><td>Compare Validator Example</td></tr><tr><td> </td><td> </td></tr><tr><td align="right">Enter Password :</td><td><asp:TextBox ID="txtpassword" runat="server"></asp:TextBox></td></tr><tr><td align="right">Confirm Password:</td><td><asp:TextBox ID="txtconfpassword" runat="server"></asp:TextBox><asp:CompareValidator ID="CompareValidator1" runat="server"ErrorMessage="Password not Marches Try again.!"ControlToCompare="txtpassword" ControlToValidate="txtconfpassword"ValueToCompare="vg1"></asp:CompareValidator><br /></td></tr><tr><td> </td><td><asp:Button ID="Button1" runat="server" Text="Submit" ValidationGroup="vg1" /></td></tr></table></div></form></body></html> |
Output |
Input the same text in both fields: |