CompareValidator control in ASP.NET

In this article we will learn how to use CompareValidator in ASP.NET.
  • 2883

In this article we will learn how to use CompareValidator in ASP.NET.


Validation control are Used to validate data inside the controls. The CompareValidator control is used to compare the value of one input control to the value of another input control or to a fixed value.

Properties: CompareValidator control has the following properties.


Figure 1.

Type - Specifies the data type of the values to compare. The types are:
  • Currency
  • Date
  • Double
  • Integer
  • String

Text  - Text to display for the validator when the validated control is invalid.

BackColor - The background color of the RequiredFieldValidator control.

ControlToValidate - The id of the control to validate.

ControlToCompare - This property are used to compare with the id of the other control.

Display - The display behavior for the validation control. Legal values are:

-None (the control is not displayed. Used to show the error message only in the ValidationSummary control).

-Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.

-Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation.

ErrorMessage - The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set.

For example:

Drag three Label, two Textbox, one Button, three RequiredFieldValidator control and one CompareValidator from the toolbox on the form. Form looks like this.


Figure 2.

Now click on the source button of the design form.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm3.aspx.vb" Inherits="WebApplication31.WebForm3" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">


<html xmlns="">

<head runat="server">




    <form id="form1" runat="server">



        <asp:TextBox ID="txtLogin" runat="server"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

            ControlToValidate="txtLogin" ErrorMessage="Login is required"

            ForeColor="Red" SetFocusOnError="True"></asp:RequiredFieldValidator>



        Password<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"


            ErrorMessage="Password Required" ForeColor="Red" SetFocusOnError="True"></asp:RequiredFieldValidator>



        Retype Password<asp:TextBox ID="txtRePassword" runat="server"


        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"

            ControlToValidate="txtRePassword" Display="Dynamic"

            ErrorMessage="Retype the Password" ForeColor="Red" SetFocusOnError="True"></asp:RequiredFieldValidator>

        <asp:CompareValidator ID="CompareValidator1" runat="server"

            ControlToCompare="txtPassword" ControlToValidate="txtRePassword"

            ErrorMessage="Password Mismatch" ForeColor="#000099"





        <asp:Button ID="Button1" runat="server" Text="Save" />






Now set the above properties of the control and run the application.


Figure 3.

Now we click on the Button(validate) without write anything in the TextBox. It display an error. such as


Figure 4.

Now enter the login name and different password in textbox. It will also display an error.

Figure 5.

Now enter the login name and same password in both textbox and click on the button. It will execute without error.

Figure 6.


More Articles

© 2020 DotNetHeaven. All rights reserved.