ASP.NET CompareValidator Control
Validation Server Controls
Definition and Usage
The CompareValidator control is used to compare the value of one input control with the value of another input control or a constant value.
**Note:** If the input control is empty, no validation function is called and validation succeeds. Use the RequiredFieldValidator control to make a field required.
property
| property | Description |
|---|---|
| BackColor | The background color of the CompareValidator control. |
| ControlToCompare | The name of the control to compare with the control being validated. |
| ControlToValidate | The id of the control to be validated. |
| Display | The display behavior of the validation control. Valid values are:
|
- Equal
- GreaterThan
- GreaterThanEqual
- LessThan
- LessThanEqual
- NotEqual
- DataTypeCheck
- Currency
- Date
- Double
- Integer
- String
Example
CompareValidator
In this example, we declare two TextBox controls, one Button control, and one CompareValidator control in the .aspx file. If validation fails, the text "Validation Failed!" is displayed in the CompareValidator control with a yellow background and red text.
CompareValidator 2
In this example, we declare two TextBox controls, one ListBox control, one Button control, and one CompareValidator control in the .aspx file. The check_operator() function sets the operator selected in the ListBox control as the operator of the CompareValidator control, then validates the CompareValidator control. If validation fails, the text "Validation Failed!" is displayed in the CompareValidator control with a yellow background and red text.
Validation Server Controls