ASP.NET RangeValidatorControl


Validation Server ControlsValidation Server Controls

Definition and Usage

The RangeValidator control is used to check whether the value entered by the user falls between two values. It can compare values of different types, such as numbers, dates, and characters.

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

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


Properties

Property Description
BackColor The background color of the RangeValidator control.
ControlToValidate The id of the control to validate.
Display The display behavior of the validation control. Valid values are:
  • None - The control is not displayed. It is only used to display error messages in the ValidationSummary control.
  • Static - If validation fails, the control displays an error message. Even if the input passes validation, space for displaying the message is reserved on the page, i.e., the space used for displaying the message is pre-allocated.
  • Dynamic - If validation fails, the control displays an error message. If the input passes validation, no space is reserved on the page for displaying the message, i.e., the space for displaying the message is added dynamically.
EnableClientScript Boolean value that specifies whether client-side validation is enabled.
Enabled Boolean value that specifies whether the validation control is enabled.
ErrorMessage The text to display 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 control specified by ControlToValidate passes validation.
MaximumValue Specifies the maximum value of the input control.
MinimumValue Specifies the minimum value of the input control.
runat Specifies that the control is a server control. Must be set to "server".
Type Specifies the data type of the value to be checked. Types are:
  • Currency
  • Date
  • Double
  • Integer
  • String
Text The message to display when validation fails.


Examples

RangeValidator
In this example, we declare a TextBox control, a Button control, and a RangeValidator control in a .aspx file. If validation fails, the text "The date must be between 1/1/2002 and 31/5/2002!" will be 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 a .aspx file. The submit() function can detect whether the page is valid. If it is valid, it returns "The page is valid!" in the Label control. If it is invalid, it returns "The page is not valid!" in the Label control. If validation fails, "The value must be from 1 to 100!" is displayed in the RangeValidator control.


Validation Server ControlsValidation Server Controls Other Extensions