Compartilhar via


How to: Validate Against a Specific Value for ASP.NET Server Controls

Using ASP.NET validation controls, you can validate a user's entry against a specific value using logical operators. For example, you can specify that the user's entry is a date after January 1, 1950 or that it is an integer value greater than or equal to zero. Alternatively, you can specify that the user's entry be compared against a value from another control.

Security noteSecurity Note

User input in ASP.NET Web pages can include potentially malicious client script. For more information, see Script Exploits Overview.

To validate against a specific value

  1. Add a CompareValidator control to the page and set the following properties:

    Property

    Description

    ControlToValidate

    The ID of the control for which the user must provide a value.

    ErrorMessage, Text, Display

    Properties that specify the text and location of the error or errors that will appear if the user skips the control. For details, see How to: Control Validation Error Message Display for ASP.NET Server Controls.

  2. Set the value to compare to by setting the following properties:

    Property

    Description

    ValueToCompare

    - or -

    ControlToCompare

    An expression entered as a string. To compare to a constant value, set the ValueToCompare property. To compare against the value of another control, set the ControlToCompare property to the ID of that control. (The CompareValidator control compares the user's entry against whatever property is specified by the other control's ValidationPropertyAttribute). If you set both ValueToCompare and ControlToCompare, ControlToCompare takes precedence.

    Type

    The data type of the two values to be compared. Types are specified using the ValidationDataType enumeration, which enables you to use the type name String, Integer, Double, Date, or Currency. The values are converted to this type before the comparison is performed. For more information, see How to: Validate Against a Data Type for ASP.NET Server Controls.

    Operator

    The comparison to use. Specify an operator using one of the following values defined in the ValidationCompareOperator enumeration:

    Note

    When validation is done against another control, invalid values in the other control are ignored and the validation passes. For details, see Special-Case Validation Results for ASP.NET Server Controls.

  3. Add a test in your code to check for validity. For details, see How to: Test Validity Programmatically for ASP.NET Server Controls.

    The following code example shows a TextBox control validated using a CompareValidator control:

    <table>
      <tr>
        <td>
          <asp:Textbox id="txtAge" runat="server"></asp:Textbox>
        </td>
        <td>
          <asp:CompareValidator id="CompareFieldValidator1" runat="server"
            ForeColor="Red"
            ControlToValidate="txtAge"
            ValueToCompare="0"
            Type="Integer"
            Operator="GreaterThanEqual"
            ErrorMessage="Please enter a whole number zero or greater.">
          </asp:CompareValidator >
        </td>
      </tr>
    </table>
    
    
    <table>
      <tr>
        <td>
          <asp:Textbox id="txtAge" runat="server"></asp:Textbox>
        </td>
        <td>
          <asp:CompareValidator id="CompareFieldValidator1" runat="server"
            ForeColor="Red"
            ControlToValidate="txtAge"
            ValueToCompare="0"
            Type="Integer"
            Operator="GreaterThanEqual"
            ErrorMessage="Please enter a whole number zero or greater.">
          </asp:CompareValidator >
        </td>
      </tr>
    </table>
    
    

    The following code example shows a CompareValidator that compares the user's entry against the value in another control. The page enables users to make reservations at a hotel. The validator checks that the user does not enter a departure date earlier than the arrival date. (In a real application, the departure date would be required and validated as a date as well.)

    <table>
      <tr>
        <td>
          Arrive<asp:Textbox id="txtArrivalDate" runat="server"></asp:Textbox>
          Depart<asp:Textbox id="txtDepartureDate" runat="server"></asp:Textbox>
        </td>
        <td>
          <asp:CompareValidator id="CompareValidator1" runat="server"
            ForeColor="Red"
            ControlToValidate="txtDepartureDate"
            ControlToCompare="txtArrivalDate"
            Type="Date"       
            Operator="GreaterThanEqual"      
            ErrorMessage="Departure date cannot be earlier than arrival date.">
          </asp:CompareValidator >
        </td>
      </tr>
    </table>
    
    
    <table>
      <tr>
        <td>
          Arrive<asp:Textbox id="txtArrivalDate" runat="server"></asp:Textbox>
          Depart<asp:Textbox id="txtDepartureDate" runat="server"></asp:Textbox>
        </td>
        <td>
          <asp:CompareValidator id="CompareValidator1" runat="server"
            ForeColor="Red"
            ControlToValidate="txtDepartureDate"
            ControlToCompare="txtArrivalDate"
            Type="Date"       
            Operator="GreaterThanEqual"      
            ErrorMessage="Departure date cannot be earlier than arrival date.">
          </asp:CompareValidator >
        </td>
      </tr>
    </table>
    
    

See Also

Concepts

Types of Validation for ASP.NET Server Controls

Other Resources

ASP.NET Validation Controls