How to: Add and Configure a CompareValidator ControlĀ 

Use the CompareValidator control to validate input data in TextBox and SelectionList controls. The CompareValidator control compares the input data to the data in another control. It can also compare the input data to a literal value that you specify.

NoteNote

If the user leaves the targeted control blank, the control passes the comparison validation. To force the user to enter a value, add a RequiredFieldValidator control as well.

Add and Configure a CompareValidator Control

  1. Add a CompareValidator control to the form that contains the control to validate.

  2. Set the ControlToValidate property to the ID of the control to validate.

  3. Set the Type property to the type of the data to be validated.

  4. Select the value to be compared to the input data in the target control using one of the following methods:

    • Set the ControlToCompare property to the ID of the control that you want to use for comparison.

    • Enter a literal value to compare to in the ValueToCompare property.

      NoteNote

      If you enter values in both the ControlToCompare and ValueToCompare properties, the ControlToCompare property takes precedence.

  5. Specify a string in the ErrorMessage property for the CompareValidator control to display if validation fails.

    NoteNote

    If you enter a string into the Text property, the CompareValidator control displays that text when an error occurs. If the Text property is empty, the CompareValidator control displays the string in the ErrorMessage property.

  6. Set the Operator property to one of the following comparison operations: Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, or DataTypeCheck. Use the DataTypeCheck enumeration value if you want to validate against a data type.

See Also

Tasks

How to: Add and Configure a CustomValidator Control
How to: Add and Configure a RangeValidator Control
How to: Add and Configure a RegularExpressionValidator Control
How to: Add and Configure a RequiredFieldValidator Control
How to: Add and Configure a ValidationSummary Control

Concepts

Introduction to the Validation Controls