Partilhar via


CompareValidator Control (General Reference) 

Evaluates the value of an input control against a constant value or the value of another input control to determine whether the two values match the relationship specified by a comparison operator (less than, equal to, greater than, and so on).

<asp:CompareValidator
    AccessKey="string"
    AssociatedControlID="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    ControlToCompare="string"
    ControlToValidate="string"
    CssClass="string"
    CultureInvariantValues="True|False"
    Display="None|Static|Dynamic"
    EnableClientScript="True|False"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    ErrorMessage="string"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    Operator="Equal|NotEqual|GreaterThan|GreaterThanEqual|LessThan|
        LessThanEqual|DataTypeCheck"
    runat="server"
    SetFocusOnError="True|False"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    ToolTip="string"
    Type="String|Integer|Double|Date|Currency"
    ValidationGroup="string"
    ValueToCompare="string"
    Visible="True|False"
    Width="size"
/>

Remarks

The CompareValidator control allows you to compare the value entered by the user into an input control, such as a TextBox control, with the value entered into another input control, or with a constant value. You can also use the CompareValidator control to determine whether the value entered into an input control can be converted to the data type specified by the 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 with the name of the control to compare.

Instead of comparing the values of two input controls, 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.

The Operator property allows you 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 both the ControlToCompare and ValueToCompare properties and simply indicates whether the value entered into the input control can be converted to the data type specified by the Type property.

NoteNote

If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to prevent the user from skipping an input control.

For more information on the CompareValidator control, see the System.Web.UI.WebControls.CompareValidator class.

Example

The following code example demonstrates how to use the CompareValidator control to validate whether the value entered in a text box matches the value entered in another text box. The validation result is then displayed on the page.

<%@ Page Language="VB" AutoEventWireup="True" %>
 
<html>
<head>
   <script runat="server">
 
      Sub Button_Click(sender As Object, e As EventArgs) 
 
         If Page.IsValid Then 
         
            lblOutput.Text = "Result: Valid!"
         
         Else 
         
            lblOutput.Text = "Result: Not valid!"
         
         End If

      End Sub
 
      Sub Operator_Index_Changed(sender As Object, e As EventArgs) 

         Compare1.Operator = CType(ListOperator.SelectedIndex, ValidationCompareOperator)
         Compare1.Validate()

      End Sub

      Sub Type_Index_Changed(sender As Object, e As EventArgs) 

         Compare1.Type = CType(ListType.SelectedIndex, ValidationDataType)
         Compare2.Type = CType(ListType.SelectedIndex, ValidationDataType)
         Compare1.Validate()

      End Sub
 
   </script>
 
</head>
<body>
 
   <form runat="server">

      <h3>CompareValidator Example</h3>
      <p>
      Enter a value in each text box. 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"/>

               <br>

               <asp:CompareValidator id="Compare1" 
                    ControlToValidate="TextBox1" 
                    ControlToCompare="TextBox2" 
                    Type="String"
                    EnableClientScript="false" 
                    Text="Failed Validation" 
                    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"/>
               <br>
               <asp:CompareValidator id="Compare2" 
                    ControlToValidate="TextBox2"  
                    Operator="DataTypeCheck"
                    EnableClientScript="false"
                    Text="Invalid Data Type" 
                    runat="server"/>
               <br>
               <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:ListBox>
            </td>
         </tr>
      </table>
 
      <br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>
 
   </form>
 
</body>
</html>
<%@ 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;
         Compare2.Type = (ValidationDataType)ListType.SelectedIndex;
         Compare1.Validate();

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

      <h3>CompareValidator Example</h3>
      <p>
      Enter a value in each text box. 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"/>

               <br>

               <asp:CompareValidator id="Compare1" 
                    ControlToValidate="TextBox1" 
                    ControlToCompare="TextBox2" 
                    Type="String"
                    EnableClientScript="false" 
                    Text="Failed Validation" 
                    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"/>
               <br>
               <asp:CompareValidator id="Compare2" 
                    ControlToValidate="TextBox2"  
                    Operator="DataTypeCheck"
                    EnableClientScript="false"
                    Text="Invalid Data Type" 
                    runat="server"/>
               <br>
               <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:ListBox>
            </td>
         </tr>
      </table>
 
      <br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>
 
   </form>
 
</body>
</html>

See Also

Reference

CompareValidator

Other Resources

Validation Server Control Syntax