Export (0) Print
Expand All

RangeValidator Class

Checks whether the value of an input control is within a specified range of values.

For a list of all members of this type, see RangeValidator Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.WebControl
         System.Web.UI.WebControls.Label
            System.Web.UI.WebControls.BaseValidator
               System.Web.UI.WebControls.BaseCompareValidator
                  System.Web.UI.WebControls.RangeValidator

[Visual Basic]
Public Class RangeValidator
   Inherits BaseCompareValidator
[C#]
public class RangeValidator : BaseCompareValidator
[C++]
public __gc class RangeValidator : public BaseCompareValidator
[JScript]
public class RangeValidator extends BaseCompareValidator

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The RangeValidator control tests whether the value of an input control is within a specified range.

The RangeValidator control uses four key properties to perform its validation. The ControlToValidate property contains the input control to validate. The MinimumValue and MaximumValue properties specify the minimum and maximum values of the valid range.

The BaseCompareValidator.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 the validation operation is performed. The following table lists the different data types that can be compared.

Data Type Description
String A string data type.
Integer A 32-bit signed integer data type.
Double A double-precision floating point number data type.
Date A date data type.
Currency A decimal data type that can contain currency symbols.
Note   Validation succeeds if the input control is empty. Use a RequiredFieldValidator control to make the input control a mandatory field.
Note   The RangeValidator control throws an exception if the value specified by the MaximumValue or MinimumValue property cannot be converted to the specified BaseCompareValidator.Type.

For additional information on validation controls, see BaseValidator.

Example

[Visual Basic, C#] The following example demonstrates how to create a RangeValidator control on the Web page to determine whether the value entered into an input control is within the comparison range.

[Visual Basic] 

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>

<head>

   <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 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"/>

      <br><br>

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

      <br><br>

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

   </form>

</body>
</html>


[C#] 

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

<html>

<head>

   <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 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"/>

      <br><br>

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

      <br><br>

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

   </form>

</body>
</html>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.WebControls

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

RangeValidator Members | System.Web.UI.WebControls Namespace | ControlToValidate | MaximumValue | MinimumValue | BaseCompareValidator.Type | RequiredFieldValidator | BaseValidator

Show:
© 2014 Microsoft