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. The CompareValidator control passes validation if the value of the input control matches the criteria specified by the Operator, ValueToCompare, and/or ControlToCompare properties.
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.
Important: |
|---|
| When the Type property is set to Date and the current calendar type is non-Gregorian, the validator performs server-side validation only. The validator client script supports only Gregorian calendars. |
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.
The following code example demonstrates how to use a CompareValidator control to compare the values in two TextBox controls.
Security Note: |
|---|
| This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview (Visual Studio). |
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
Note:
Security Note: