Compartilhar via


RangeValidator Control (General Reference) 

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

<asp:RangeValidator
    AccessKey="string"
    AssociatedControlID="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    ControlToValidate="string"
    CssClass="string"
    CultureInvariantValues="True|False"
    Display="None|Static|Dynamic"
    EnableClientScript="True|False"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    ErrorMessage="string"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
                Large|X-Large|XX-Large"
        Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    MaximumValue="string"
    MinimumValue="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SetFocusOnError="True|False"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    ToolTip="string"
    Type="String|Integer|Double|Date|Currency"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

Remarks

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.

NoteNote

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.

NoteNote

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.

Example

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="VB" AutoEventWireup="True" %>

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

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

   <script runat="server">

      Sub ButtonClick(sender As Object, e As EventArgs)

         If Page.IsValid Then
        
            Label1.Text="Page is valid."
         
         Else
         
            Label1.Text="Page is not valid!!"
         
         End If

      End Sub

   </script>

</head>

<body>

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

      <h3>RangeValidator Example</h3>

      Enter a number from 1 to 10:

      <br />

      <asp:TextBox id="TextBox1"
           runat="server"/>

      <br />

      <asp:RangeValidator id="Range1"
           ControlToValidate="TextBox1"
           MinimumValue="1"
           MaximumValue="10"
           Type="Integer"
           EnableClientScript="false"
           Text="The value must be from 1 to 10!"
           runat="server"/>

      <p />

      <asp:Label id="Label1"
           runat="server"/>

      <p />

      <asp:Button id="Button1"
           Text="Submit"
           OnClick="ButtonClick"
           runat="server"/>
            

   </form>

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

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

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

   <script runat="server">

      void ButtonClick(Object sender, EventArgs e)
      {

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

      }

   </script>

</head>

<body>

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

      <h3>RangeValidator Example</h3>

      Enter a number from 1 to 10:

      <br />

      <asp:TextBox id="TextBox1"
           runat="server"/>

      <br />

      <asp:RangeValidator id="Range1"
           ControlToValidate="TextBox1"
           MinimumValue="1"
           MaximumValue="10"
           Type="Integer"
           EnableClientScript="false"
           Text="The value must be from 1 to 10!"
           runat="server"/>

      <p />

      <asp:Label id="Label1"
           runat="server"/>

      <p />

      <asp:Button id="Button1"
           Text="Submit"
           OnClick="ButtonClick"
           runat="server"/>
            

   </form>

</body>
</html>   

See Also

Reference

RangeValidator

Other Resources

Validation Server Control Syntax