CompareValidator
Collapse the table of content
Expand the table of content

CompareValidator

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. 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 panel 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 panel, 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 Cc295090.xtlink_newWindow(en-us,Expression.40).png 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.

NoteNote

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 Cc295090.xtlink_newWindow(en-us,Expression.40).png in the MSDN library.

For more information about using validation controls to validate user input, see How-to Topics – Validation Controls Cc295090.xtlink_newWindow(en-us,Expression.40).png in the MSDN library.

See also

Send feedback about this topic to Microsoft. © 2011 Microsoft Corporation. All rights reserved.

Community Additions

ADD
Show:
© 2016 Microsoft