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.

CompareValidator:

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.

cp1.gif
 

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.

cp2.gif
 

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

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

    <p>

        Login

        <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>

    </p>

    <p>

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

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

            ControlToValidate="txtRePassword"

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

    </p>

    <p>

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

            TextMode="Password"></asp:TextBox>

        <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"

            SetFocusOnError="True"></asp:CompareValidator>

    </p>

    <div>

 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

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

    </div>

    </form>

</body>

</html>

 

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

cp3.gif
 

Figure 3.
 

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

cp4.gif
 

Figure 4.
 

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

cp5.gif
Figure 5.

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

cp6.gif
Figure 6.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.