ASP.NET RangeValidator Control

Definition and Usage

The RangeValidator control is used to detect whether the user's input value is between two values. It can compare different types of values, such as numbers, dates, and characters.

Note:If the input control is empty, the validation will not fail. Please use the RequiredFieldValidator control to make the field a required field.

Note:If the input value cannot be converted to the specified data type, the validation will not fail. Please use the CompareValidator control and set its Operator property to ValidationCompareOperator.DataTypeCheck to check the data type of the input value.

Property

Property Description
BackColor Background color of the RangeValidator control
ControlToValidate ID of the control to be validated
Display

Display behavior of the validation control.

Valid values include:

  • None - The validation message is never displayed inline.
  • Static - The space for displaying the validation message is allocated in the page layout.
  • Dynamic - If validation fails, the space for displaying the validation message will be dynamically added to the page.
EnableClientScript Boolean value, specifying whether to enable client-side validation.
Enabled Boolean value, specifying whether to enable the validation control.
ErrorMessage

The text displayed in the ValidationSummary control when validation fails.

Note: If the Text property is not set, the text will also be displayed in the validation control.

ForeColor The foreground color of the control.
id The unique id of the control.
IsValid Boolean value indicating whether the associated input control passes the validation.
MaximumValue Specify the maximum value of the input control.
MinimumValue Specify the minimum value of the input control.
runat Specify that this control is a server control. It must be set to 'server'.
Type

Specify the data type to be checked. The types are:

  • Currency
  • Date
  • Double
  • Integer
  • String
Text The message displayed when validation fails.

Example

RangeValidator
In this example, we declare a TextBox control, a Button control, and a RangeValidator control in the .aspx file. If the validation fails, the text 'The date must be between 1/1/2002 and 31/5/2002!' is displayed in the RangeValidator control.
RangeValidator 2
In this example, we declare a TextBox control, a Button control, a Label control, and a RangeValidator control in the .aspx file. The submit() function can check if the page is valid. If it is valid, 'The page is valid!' is returned in the Label control. If it is invalid, 'The page is not valid!' is returned in the Label control. If the validation fails, 'The value must be from 1 to 100!' is displayed in the RangeValidator control.