Compare field validator - DotNet and DataBase

Follow us on Facebook

SQL Server Mountain View Mountain View Mountain View

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.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldvalidator.aspx.cs" Inherits="WebProgramming.Validators.RequiredFieldvalidator" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
                      UserName:  <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="UserName is required." ControlToValidate="txtUserName"></asp:RequiredFieldValidator></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>
                      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>
                      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>
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit"  /></td>


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


No comments:

Post a comment


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.