Introduction:

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

Description:

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:

CompareValidator.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CompareValidator.aspx.cs"
 
Inherits="AspDotNet.CompareValidator" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Password</td>
                    <td>
                        <asp:TextBox runat="server" ID="txtpassword" TextMode="Password" />
</td>
                </tr>
                <tr>
                    <td>Confirm Password</td>
                    <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!" />
                    </td>
                </tr>
                <tr>
                    <td>
<asp:Button runat="server" ID="btnSubmit" Text="Submit" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

 

Output:


How to use CompareValidator in Asp.Net


I hope this article will help to you.

0 comments


Leave a comment

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

You may also like