CompareValidator Example using ASP.NET | Password Comparison using CompareValidator

CompareValidator Example using ASP.NET | Password Comparison using CompareValidator

In this example, we are going to learn how to use CompareValidator Server side validation control. CompareValidator is useful for comparing two fields in the form. In the below code i have compared password and confirm password filed. If password filed and compare password is not same. This will through an error. Please check below output for more details.

CompareValidator required ControlToValidate, ControlToCompare, id, error message and run at attributes.

Output:
CompareValidator Example using ASP.NET | Password Comparison using CompareValidator
CompareValidator Example using ASP.NET | Password Comparison using CompareValidator

Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CompareValidator.aspx.cs" Inherits="CompareValidator" %>

<!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">
    <div>
    
        <table style="width:50%;">
            <tr>
                <td>
                    Password</td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Confirm Password</td>
                <td>
                    <asp:TextBox ID="txtConfirmPassword" runat="server"></asp:TextBox>
                    <asp:CompareValidator ID="CompareValidator1" runat="server" 
                        ControlToCompare="txtConfirmPassword" ControlToValidate="txtPassword" 
                        ErrorMessage="Password does not match"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnCompare" runat="server" Text="Compare Passwords" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>


Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar