ASP.NET - Validators - RangeValidator

The RangeValidator control is used to validate that the value of an input control falls within a specific range of values. Here are some of the properties, methods, and events associated with this control:

Properties:

  • ControlToValidate: This property specifies the ID of the control that the validator should validate. For example, if you have a textbox that is required, you would set the ControlToValidate property to the ID of that textbox control.
  • ErrorMessage: This property specifies the error message that should be displayed if the validation fails. This message will be displayed next to the control being validated.
  • MaximumValue: This property specifies the maximum value allowed for the input control. The value can be a number, a date, or a string, depending on the data type of the control being validated.
  • MinimumValue: This property specifies the minimum value allowed for the input control. The value can be a number, a date, or a string, depending on the data type of the control being validated.
  • Type: This property specifies the data type of the control being validated. It can be set to "Integer", "Double", "Currency", "Date", or "String".

Methods:

  • Validate: This method performs the validation check and returns a boolean value indicating whether the validation succeeded or failed.

Events:

  • ServerValidate: This event is fired when the validator is validated on the server side. You can use this event to perform custom validation logic, such as checking a value against a database.

Control:

  • RangeValidator control is derived from BaseValidator, so all its members are available.

Example 1: Validating an input control that contains a date

In this example, we will use the RangeValidator control to validate a textbox control that contains a date. The RangeValidator will ensure that the date falls within a specific range of dates.

<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:RangeValidator ID="rvDate" runat="server" ControlToValidate="txtDate" Type="Date" MinimumValue="2023-01-01" MaximumValue="2023-12-31" ErrorMessage="Please enter a date between January 1, 2023 and December 31, 2023"></asp:RangeValidator>

In this example, we have a textbox control with ID "txtDate" that we want to validate. We have created a RangeValidator control with ID "rvDate" and set its ControlToValidate property to "txtDate". We have also set the Type property to "Date" to indicate that the input control contains a date. The MinimumValue and MaximumValue properties are set to "2023-01-01" and "2023-12-31", respectively, to specify the allowed range of dates. Finally, we have set the ErrorMessage property to "Please enter a date between January 1, 2023 and December 31, 2023" to display an error message if the validation fails.

Example 2: Validating an input control that contains a number

In this example, we will use the RangeValidator control to validate a textbox control that contains a number. The RangeValidator will ensure that the number falls within a specific range of values.

<asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
<asp:RangeValidator ID="rvNumber" runat="server" ControlToValidate="txtNumber" Type="Double" MinimumValue="0" MaximumValue="100" ErrorMessage="Please enter a number between 0 and 100"></asp:RangeValidator>

In this example, we have a textbox control with ID "txtNumber" that we want to validate. We have created a RangeValidator control with ID "rvNumber" and set its ControlToValidate property to "txtNumber". We have also set the Type property to "Double" to indicate that the input control contains a number. The MinimumValue and MaximumValue properties are set to 0 and 100, respectively, to specify the allowed range of values. Finally, we have set the ErrorMessage property to "Please enter a number between 0 and 100" to display an error message if the validation fails.