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.
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.
Add and Configure a CompareValidator Control
Add a CompareValidator control to the form that contains the control to validate.
Set the ControlToValidate property to the ID of the control to validate.
Set the Type property to the type of the data to be validated.
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.
Note
If you enter values in both the ControlToCompare and ValueToCompare properties, the ControlToCompare property takes precedence.
Specify a string in the ErrorMessage property for the CompareValidator control to display if validation fails.
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 empty, the CompareValidator control displays the string in the ErrorMessage property.
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