-->

22 December 2019

Asp.Net CompareValidator Control

  Asp.Net CS By Example       22 December 2019


   The CompareValidator :
     
  • The CompareValidator control enables we to perform three different types of validation tasks. we can use the CompareValidator to perform a data type check.

  • Syntax:
    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtAge" Operator="DataTypeCheck" Type="Integer" ErrorMessage="|| (Invalid Age || " Display="None" ValidationGroup="Validation" SetFocusOnError="true" />



    The CompareValidator has six important properties:
    Sr.No Property Description
    1) ID Set the uniqe value to control to access in code-behind code.
    2) ControlToValidate which control is check or The ID of the form field validated.
    3) Text The error message displayed when validation fails.
    4) Operator The type of comparison to perform. Possible values are DataTypeCheck, Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and NotEqual.
    5) ValueToCompare The fixed value against which to compare.
    6) Type The type of value compared. Possible values are String, Integer, Double,Date, and Currency.

    Code: CompareValidatorDemo.aspx

    <%@ Page Language="C#" AutoEventWireup="true"  %>
    
    <!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 id="Head1" runat="server">
        <title></title>
         <style>
            .column
            {
                float: left;
                width: 30%;
                margin-left: 10px;
                background-color: white;
                border: solid 1px black;
                padding: 10px;
                width: 70%;
            }
            fieldset
            {
                background-color: #a9a9a9;
                border: solid 2px #d81a72;
                color:Black;
                width:93%;
            }
        </style>
    </head>
    <body style="width: 70%;">
        <form id="form1" runat="server" >
        <div class="column">
            <fieldset>
                <legend>CompareValidator Demo</legend>
                <asp:Label ID="Label1" Text="Start Date:" runat="server" />
                <asp:TextBox ID="txtStartDt" runat="server" ValidationGroup="CompareValidator" />
                <br />
                <br />
                <asp:Label ID="Label2" Text="End Date:" runat="server" />
                <asp:TextBox ID="txtEndDt" runat="server" ValidationGroup="CompareValidator" />
                <asp:CompareValidator ID="cvDateCompare" ErrorMessage="(End date must be greater than start date)"
                    ValidationGroup="CompareValidator" ControlToValidate="txtStartDt" ControlToCompare="txtEndDt"
                    EnableClientScript="true" Type="Date" Operator="LessThan" SetFocusOnError="true"
                    runat="server" />
                <br />
                <asp:Button ID="btncvSubmit" Text="Submit" runat="server" ValidationGroup="CompareValidator" />
                <asp:Button ID="btncvReset" Text="Reset" runat="server" ValidationGroup="CompareValidator"
                    CausesValidation="false" />
            </fieldset>
        </div>
        
    
        </form>
    </body>
    </html>
    
    
                    

    Output:

    logoblog

    Thanks for reading Asp.Net CompareValidator Control

    Previous
    « Prev Post