CompareValidator


The CompareValidator is used to compare the user's inputs entered in input box with another Inputbox.

 

The best example can be considered here is password and confirm password. While creating an online registration on websites many times seen required to enter password and confirm password and to complete the process the password and confirm password require same. 

 

The following namespace is necessary to use the CompareValidator control in the Asp.Net, C#:

 

System.Web.UI.WebControls

 

Syntax

 

The following is the syntax of CompareValidator control in asp.net :

 

<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator" ></asp:CompareValidator>



The following are the CompareValidator control main events in the Asp.net,C#:


Event Description
DataBinding It happens when the server control binds to a data source.
Load It happens when the server control is fully loaded into the web Page object.
Unload It happens when the server control is fully unloaded from memory.
PreRender It happens after the control attribute is full but previous to rendering.
Disposed It happens when a server control is freed from memory, which is the last phase of the server control development when an ASP.NET page is invited.
Init It happens while the server control is prepared, that is the primary step in its lifecycle.  

In this chapter, we will guide the CompareValidator with the example of password and confirm password.




Example



CompareValidator.aspx

<!DOCTYPE html>

 

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

<head runat="server">

</head>

<body>

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

    <p>

       <h2> Asp.net CompareValidator Example</h2>

 

 

<asp:Label ID="Label1"  runat="server" Font-Bold="True" ForeColor="#003300"></asp:Label>

       

        <asp:CompareValidator ID="CompareValidator1"  runat="server" ErrorMessage="Password &amp; Confirm Password not match !" Font-Bold="True" ForeColor="Red" ControlToValidate ="TextBox1" ControlToCompare ="TextBox2">

</asp:CompareValidator>

 

        <table >

            <tr>

                <td>Password</td>

                <td>

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

                </td>

            </tr>

            <tr>

                <td>Confirm Password</td>

                <td>

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

                </td>

            </tr>

        </table >

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Register" />

    </form>

</body>

</html>

 


CompareValidator.aspx

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)

        {

      

        }

    protected void Button1_Click(object sender, EventArgs e)

    {

      Label1.Text = "Thank you for register !";

    }



 View Result