RangeValidator Control (General Reference)

Evaluates the value of an input control to determine whether it is between the specified upper and lower boundaries.

    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    ForeColor="color name|#dddddd"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"

The RangeValidator control allows you to check whether a user's entry is between a specified upper and a specified lower boundary. You can check ranges within pairs of numbers, alphabetic characters, and dates. Boundaries are expressed as constants.

Use the ControlToValidate property to specify the input control to validate. The MinimumValue and MaximumValue properties specify the minimum and maximum values of the valid range, respectively.

The Type property is used to specify the data type of the values to compare. The values to compare are converted to this data type before any comparison is performed.


If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to prevent the user from skipping an input control.


The RangeValidator control throws an exception if the value specified by the MaximumValue or MinimumValue property cannot be converted to the data type specified by the Type property. For example, when a RangeValidator control's Type property is set to "Currency", the MinimumValue and MaximumValue properties must be provided in a format such as that described in System.Globalization.NumberFormatInfo.CurrencyDecimalDigits, otherwise an exception is thrown.

For more information on the RangeValidator control, see the System.Web.UI.WebControls.RangeValidator class.

The following code example demonstrates how to use the RangeValidator control to validate whether the value entered in a text box is between one and ten. The validation result is then displayed on the page.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

 <%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >

<title>RangeValidator Control (General Reference)</title>

   <script runat="server">

      void ButtonClick(Object sender, EventArgs e)

         if (Page.IsValid)
            Label1.Text="Page is valid.";
            Label1.Text="Page is not valid!!";





   <form id="Form1" runat="server">

      <h3>RangeValidator Example</h3>

      Enter a number from 1 to 10:

      <br />

      <asp:TextBox id="TextBox1"

      <br />

      <asp:RangeValidator id="Range1"
           Text="The value must be from 1 to 10!"

      <p />

      <asp:Label id="Label1"

      <p />

      <asp:Button id="Button1"



Community Additions