This documentation is archived and is not being maintained.

Adding and Configuring a CompareValidator Control

Use the CompareValidator control to validate input data in the 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.

Note   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.
  1. Add a CompareValidator control to the form that contains the control to validate.
  2. Use the drop-down list next to the ControlToValidate property in the Properties window to select the ID of the control to validate.
  3. Use the Type property to select 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:
    • Use the drop-down list next to the ControlToCompare property in the Properties window to select the ID of the control that you want to use for comparison.
    • Enter a value to compare to in the ValueToCompare property.
    Note   If you enter values in both the ControlToCompare and ValueToCompare properties, the CompareValidator control uses the ControlToCompare property and ignores the ValueToCompare property.
  5. Type a text string into the ErrorMessage property in the Properties window. The CompareValidator control displays this text when the control being validated fails the test.
    Note   If you enter a string into the Text property, the CompareValidator control displays that text when an error occurs. If the Text property is left empty, the CompareValidator control displays the string in the ErrorMessage property.
  6. Use the drop-down list next to the Operator property to select one of the following comparison operations: Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, or DataTypeCheck. Use the DataTypeCheck operator if you want to validate against a data type.

See Also

Introduction to the Validation Controls | Adding and Configuring a CustomValidator Control | Adding and Configuring a RangeValidator Control | Adding and Configuring a RegularExpressionValidator Control | Adding and Configuring a RequiredFieldValidator Control | Adding and Configuring a ValidationSummary Control

Show: