Export (0) Print
Expand All
0 out of 4 rated this helpful - Rate this topic

CompareValidator Class

Compares the value entered by the user in an input control with the value entered in another input control, or with a constant value.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class CompareValidator : BaseCompareValidator
public class CompareValidator extends BaseCompareValidator
public class CompareValidator extends BaseCompareValidator

Use the CompareValidator control to compare the value entered by the user in an input control, such as a TextBox control, with the value entered in another input control or a constant value. You can also use the CompareValidator control to indicate whether the value entered in an input control can be converted to the data type specified by the BaseCompareValidator.Type property.

Specify the input control to validate by setting the ControlToValidate property. If you want to compare a specific input control with another input control, set the ControlToCompare property to specify the control to compare with.

NoteNote

If the value entered in the input control specified by the ControlToCompare property cannot be converted to the data type specified by the BaseCompareValidator.Type property, but the value entered in the input control specified by the ControlToValidate property can be converted, the input control being validated is considered valid. Therefore, you should also place a CompareValidator or RangeValidator control on the input control specified by the ControlToCompare property.

Instead of comparing the value of an input control with another input control, you can compare the value of an input control to a constant value. Specify the constant value to compare with by setting the ValueToCompare property.

NoteNote

If the value specified by the ValueToCompare property cannot be converted to the data type specified by the BaseCompareValidator.Type property, an exception is thrown. Be sure to check the data type of a value before programmatically assigning it to the ValueToCompare property.

NoteNote

Do not set both the ControlToCompare and the ValueToCompare property at the same time. You can either compare the value of an input control to another input control, or to a constant value. If both properties are set, the ControlToCompare property takes precedence.

Use the Operator property to specify the type of comparison to perform, such as greater than, equal to, and so on. If you set the Operator property to ValidationCompareOperator.DataTypeCheck, the CompareValidator control ignores the ControlToCompare and ValueToCompare properties and simply indicates whether the value entered in the input control can be converted to the data type specified by the BaseCompareValidator.Type property.

The BaseCompareValidator.Type property is used to specify the data type of both comparison values. Both values are automatically converted to this data type before the comparison operation is performed. The following table lists the various data types that can be compared.

Data type

Description

String

A string data type.

Integer

A 32-bit signed integer data type.

Double

A double-precision floating-point number data type.

Date

A date data type.

Currency

A decimal data type that can contain currency symbols.

NoteNote

If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.

For more information on validation controls, see BaseValidator.

Accessibility

The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

TopicLocation
How to: Validate Against Values in a Database for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Range of Values for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Specific Value for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Data Type for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Specify Layout for In-Place Messages On ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Required Entries for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Display Server Side Custom Validation Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Test Validity Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Patterns for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Format Validation Error Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate with a Custom Function for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Disable Validation for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Values in a Database for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Range of Values for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Specific Value for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Data Type for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Specify Layout for In-Place Messages On ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Required Entries for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Display Server Side Custom Validation Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Test Validity Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Patterns for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Format Validation Error Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate with a Custom Function for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Disable Validation for ASP.NET Server ControlsBuilding ASP .NET Web Applications

The following code example demonstrates how to use a CompareValidator control to compare the values in two TextBox controls.

<%@ Page Language="C#" AutoEventWireup="True" %>
 
<html>
<head>
   <script runat="server">
 
      void Button_Click(Object sender, EventArgs e) 
      {
 
         if (Page.IsValid) 
         {
            lblOutput.Text = "Result: Valid!";
         }
         else 
         {
            lblOutput.Text = "Result: Not valid!";
         }

      }
 
      void Operator_Index_Changed(Object sender, EventArgs e) 
      {

         Compare1.Operator = (ValidationCompareOperator) ListOperator.SelectedIndex;
         Compare1.Validate();

      }

      void Type_Index_Changed(Object sender, EventArgs e) 
      {

         Compare1.Type = (ValidationDataType) ListType.SelectedIndex;
         Compare1.Validate();

      }
 
   </script>
 
</head>
<body>
 
   <form runat=server>

      <h3>CompareValidator Example</h3>
      <p>
      Enter a value in each textbox. Select a comparison operator<br>
      and data type. Click "Validate" to compare values.
 
      <table bgcolor="#eeeeee" cellpadding=10>

         <tr valign="top">

            <td>

               <h5>String 1:</h5>
               <asp:TextBox id="TextBox1" 
                    runat="server"/>

            </td>

            <td>

               <h5>Comparison Operator:</h5>
 
               <asp:ListBox id="ListOperator" 
                    OnSelectedIndexChanged="Operator_Index_Changed" 
                    runat="server">

                  <asp:ListItem Selected Value="Equal">Equal</asp:ListItem>
                  <asp:ListItem Value="NotEqual">NotEqual</asp:ListItem>
                  <asp:ListItem Value="GreaterThan">GreaterThan</asp:ListItem>
                  <asp:ListItem Value="GreaterThanEqual">GreaterThanEqual</asp:ListItem>
                  <asp:ListItem Value="LessThan">LessThan</asp:ListItem>
                  <asp:ListItem Value="LessThanEqual">LessThanEqual</asp:ListItem>
                  <asp:ListItem Value="DataTypeCheck">DataTypeCheck</asp:ListItem>

               </asp:ListBox>

            </td>

            <td>

               <h5>String 2:</h5>
               <asp:TextBox id="TextBox2" 
                    runat="server"/>
               <p>
               <asp:Button id="Button1"
                    Text="Validate"  
                    OnClick="Button_Click" 
                    runat="server"/>

            </td>
         </tr>

         <tr>
            <td colspan="3" align="center">

               <h5>Data Type:</h5>

               <asp:ListBox id="ListType" 
                    OnSelectedIndexChanged="Type_Index_Changed" 
                    runat="server">

                  <asp:ListItem Selected Value="String" >String</asp:ListItem>
                  <asp:ListItem Value="Integer" >Integer</asp:ListItem>
                  <asp:ListItem Value="Double" >Double</asp:ListItem>
                  <asp:ListItem Value="Date" >Date</asp:ListItem>
                  <asp:ListItem Value="Currency" >Currency</asp:ListItem>

               </asp:ListBox>
            </td>
         </tr>
      </table>
 
      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1" 
           ControlToCompare="TextBox2"
           EnableClientScript="False" 
           Type="String" 
           runat="server"/>
 
      <br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>
 
   </form>
 
</body>
</html>


System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.Label
         System.Web.UI.WebControls.BaseValidator
           System.Web.UI.WebControls.BaseCompareValidator
            System.Web.UI.WebControls.CompareValidator
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.