CompareValidator control

The CompareValidator control compares a user's entry against a constant value, against the value of another control (using a comparison operator such as less than, equal, or greater than), or for a specific data type.

A CompareValidator works in conjunction with another control, for example a TextBox control. Add the control you want to validate to your page before you add the CompareValidator so that you can easily associate the CompareValidator with it.

To add a CompareValidator control to a page

  1. Drag the CompareValidator control from the Toolbox task pane to your page.

  2. Select the CompareValidator in Design view, right-click it, and click Properties on the shortcut menu.

  3. In the Tag Properties task pane, select the control you want to validate with the CompareValidator in the ControlToValidate property dropdown.

  4. Set the ControlToCompare or ValueToCompare property to the control or value that you want to compare.

  5. Set the Operator property to the comparison you want to use: Equal, NotEqual, GreaterThan, GeaterThanEqual, LessThan, LessThanEqual, or DataTypeCheck. For information about these operators, see CompareValidator.Operator Property in the MSDN library.

  6. If you are going to use a ValidationSummary control to consolidate validation error messages, set the ValidationGroup property to the name of the group of validation controls. This can be any string, as long as all the controls in the group have the same value.


When you specify a control in the ControlToCompare property, 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.

For a full description of all CompareValidator control properties, see CompareValidator Members in the MSDN library.

For more information about using validation controls to validate user input, see How-to Topics — Validation Controls in the MSDN library.

See also


ASP.NET validation controls overview

CustomValidator control

RegularExpressionValidator control

RequiredFieldValidator control

ValidationSummary control