Home » ASP.Net Web Form CompareValidator

ASP.Net Web Form CompareValidator

by Online Tutorials Library

ASP.NET Validation

In this chapter, we will discuss about the data validation in the Web Forms. To perform validation, ASP.NET provides controls that automatically check user input and require no code. We can also create custom validation for our application.

ASP.NET validation controls

Following are the validation controls

Validator Description
CompareValidator It is used to compare the value of an input control against a value of another input control.
RangeValidator It evaluates the value of an input control to check the specified range.
RegularExpressionValidator It evaluates the value of an input control to determine whether it matches a pattern defined by a regular expression.
RequiredFieldValidator It is used to make a control required.
ValidationSummary It displays a list of all validation errors on the Web page.

ASP.NET CompareValidator Control

This validator evaluates the value of an input control against another input control on the basis of specified operator.

We can use comparison operators like: less than, equal to, greater than etc.

Note: If the input filed is empty, no validation will be performed.

CompareValidator Properties

Property Description
AccessKey It is used to set keyboard shortcut for the control.
TabIndex The tab order of the control.
BackColor It is used to set background color of the control.
BorderColor It is used to set border color of the control.
BorderWidth It is used to set width of border of the control.
Font It is used to set font for the control text.
ForeColor It is used to set color of the control text.
Text It is used to set text to be shown for the control.
ToolTip It displays the text when mouse is over the control.
Visible To set visibility of control on the form.
Height It is used to set height of the control.
Width It is used to set width of the control.
ControlToCompare It takes ID of control to compare with.
ControlToValidate It takes ID of control to validate.
ErrorMessage It is used to display error message when validation failed.
Operator It is used set comparison operator.

Example

Here, in the following example, we are validating user input by using CompareValidator controller. Source code of the example is given below.

// compare_validator_demo.aspx

Output:

ASP Validation 1
ASP Validation 2

You may also like