In this article we will learn how to use CompareValidator in or what is CompareValidator in or why use CompareValidator in with example.


The CompareValidator control enables you to compare the value entered in two input control, such as a TextBox control, for example we need to compare password while registering a new user.

CompareValidator  properties:

ControlToValidate: We can specify the id of the control to Validate.
ControlToCompare : We can specify the id of the control to compare.
Type: Specifies the Datatype to use for comapring.
Operator: It defines the type of comparision.
errormessage: It specifies Error message to be displayed by the control.

Let’s see Example:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CompareValidator.aspx.cs"
Inherits="AspDotNet.CompareValidator" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
                        <asp:TextBox runat="server" ID="txtpassword" TextMode="Password" />
                    <td>Confirm Password</td>
                 <asp:TextBox runat="server" ID="txtconfirmpassword" TextMode="Password"/>
                 <asp:CompareValidator runat="server" Style="color: red;" ID="cmppassword"
      ControlToValidate="txtconfirmpassword" ControlToCompare="txtpassword"
                          ErrorMessage="password does not match!" />
<asp:Button runat="server" ID="btnSubmit" Text="Submit" />



How to use CompareValidator in Asp.Net

I hope this article will help to you.

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like