CompareValidator Class
Assembly: System.Web (in system.web.dll)
Use the CompareValidator control to compare the value entered by the user in an input control, such as a TextBox control, with the value entered in another input control or a constant value. You can also use the CompareValidator control to indicate whether the value entered in an input control can be converted to the data type specified by the BaseCompareValidator.Type property.
Specify the input control to validate by setting the ControlToValidate property. If you want to compare a specific input control with another input control, set the ControlToCompare property to specify the control to compare with.
Note |
|---|
| If the value entered in the input control specified by the ControlToCompare property cannot be converted to the data type specified by the BaseCompareValidator.Type property, but the value entered in the input control specified by the ControlToValidate property can be converted, the input control being validated is considered valid. Therefore, you should also place a CompareValidator or RangeValidator control on the input control specified by the ControlToCompare property. |
Instead of comparing the value of an input control with another input control, you can compare the value of an input control to a constant value. Specify the constant value to compare with by setting the ValueToCompare property.
Note |
|---|
| If the value specified by the ValueToCompare property cannot be converted to the data type specified by the BaseCompareValidator.Type property, an exception is thrown. Be sure to check the data type of a value before programmatically assigning it to the ValueToCompare property. |
Note |
|---|
| Do not set both the ControlToCompare and the ValueToCompare property at the same time. You can either compare the value of an input control to another input control, or to a constant value. If both properties are set, the ControlToCompare property takes precedence. |
Use the Operator property to specify the type of comparison to perform, such as greater than, equal to, and so on. If you set the Operator property to ValidationCompareOperator.DataTypeCheck, the CompareValidator control ignores the ControlToCompare and ValueToCompare properties and simply indicates whether the value entered in the input control can be converted to the data type specified by the BaseCompareValidator.Type property.
The BaseCompareValidator.Type property is used to specify the data type of both comparison values. Both values are automatically converted to this data type before the comparison operation is performed. The following table lists the various data types that can be compared.
| Data type | Description |
|---|---|
| String | A string data type. |
| Integer | A 32-bit signed integer data type. |
| Double | A double-precision floating-point number data type. |
| Date | A date data type. |
| Currency | A decimal data type that can contain currency symbols. |
Note |
|---|
| If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control. |
For more information on validation controls, see BaseValidator.
Accessibility
The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.Label
System.Web.UI.WebControls.BaseValidator
System.Web.UI.WebControls.BaseCompareValidator
System.Web.UI.WebControls.CompareValidator
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note