This documentation is archived and is not being maintained.

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.


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.


      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.


    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