Compare field validator - DotNet and DataBase

Saturday, 18 August 2018

Compare field validator



Compare validator is to compare values in two fields or to compare one field with a fixed value.
For example, on user registration form we have the Password and Confirm password fields. The user should supply same value in fields.  Using compare field validator we can compare the value in
Confirm password field with Password field.

Example:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldvalidator.aspx.cs" Inherits="WebProgramming.Validators.RequiredFieldvalidator" %>
<!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>
                      UserName:  <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="UserName is required." ControlToValidate="txtUserName"></asp:RequiredFieldValidator></td>
                </tr>
                 <tr>
                    <td>
                      Password:       <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Password is required." ControlToValidate="txtPassword"></asp:RequiredFieldValidator></td>
                </tr>
                 <tr>
                    <td>
                      Confirm password:       <asp:TextBox ID="txtConfirmvalidator" runat="server"  TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="RangeValidator2" runat="server" ErrorMessage="Confirm password is required." ControlToValidate="txtConfirmvalidator" MinimumValue="18" MaximumValue="35"></asp:RequiredFieldValidator>
                        <asp:CompareValidator ID="cvConfirmpassword" runat="server" ControlToValidate="txtConfirmvalidator" ControlToCompare="txtPassword" ErrorMessage="Password mismatch"></asp:CompareValidator>
                    </td>
                </tr>
                 <tr>
                    <td>
                      Age:       <asp:TextBox ID="txtAge" runat="server"></asp:TextBox><asp:RangeValidator ID="rfvAge" runat="server" ErrorMessage="User age should be with in 18-35." ControlToValidate="txtAge" MinimumValue="18" MaximumValue="35"></asp:RangeValidator></td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit"  /></td>
                </tr>
            </table>
        </div>
    </form>
</body>

</html>


Here ControlToValidate="txtConfirmvalidator" is the value which must match with the  value in  ControlToCompare="txtPassword"field.

Output

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.