RangeValidator control in ASP.NET

In this article we will learn how to use RangeValidator control in ASP. NET.
  • 3466

In this article we will learn how to use RangeValidator control in ASP. NET.

RangeValidator control

Range Validators are used to test if the value of a control is inside a specified range of values.

Properties:

RangeValidator uses three key properties to perform its validation.

rv1.gif
 

Figure 1.

ControlToValidate -Gets or sets the input control to validate (eg. The ID value of asp:TextBox control). 

MinimumValue - Gets or sets the minimum value of the range.

MaximumValue - Gets or sets the maximum value of the range.

Type - Integer/String/Date/Currency/Double. Used to specify the data type to validate.

Display - The display behavior for the validation control. Legal values are:

-None (the control is not displayed. Used to show the error message only in the ValidationSummary control).

-Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.

-Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation.

ErrorMessage - The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set.

For example:

This example shows the integer range, date range and string range. the form looks like this.

rv2.gif
 

Figure 2.

select RangeValidator and set the properties of all three RangeValidator.

Now click on the source button of the design form.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="WebApplication32.WebForm2" %>

 

<!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 runat="server">

    <title></title>

</head>

<body>

    <h3><font face="Verdana">RangeValidator Sample</font></h3>

    <p>

 

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

 

      <table bgcolor="#eeeeee" cellpadding=10>

      <tr valign="top">

        <td>

            <h5><font face="Verdana">Value to Check:</font></h5>

            <asp:TextBox Selected id="txtComp1" runat="server"/>

        </td>

        <td>

            <h5><font face="Verdana">&nbsp;Integer Min(1), Max(100)</font></h5>

        </td>

        <td>

             <asp:Label id="lblOutput1" Font-Names="verdana" Font-Size="10pt" runat="server" />

        </td>

      </tr>

      <tr valign="top">

        <td>

            <h5><font face="Verdana">Value to Check:</font></h5>

            <asp:TextBox Selected id="txtComp2" runat="server"/>

        </td>

        <td>

            <h5><font face="Verdana">Date Min(2003/1/1), Max(2010/1/1)</font></h5>

        </td>

        <td>

             <asp:Label id="lblOutput2" Font-Names="verdana" Font-Size="10pt" runat="server" />

        </td>

      </tr>

      <tr valign="top">

        <td>

            <h5><font face="Verdana">Value to Check:</font></h5>

            <asp:TextBox Selected id="txtComp3" runat="server"/>

        </td>

        <td>

            <h5><font face="Verdana">String Min(Aardvark), Max(Zebra)</font></h5>

        </td>

        <td>

             <asp:Label id="lblOutput3" Font-Names="verdana" Font-Size="10pt" runat="server" />

        </td>

      </tr>

     </table>

 

     <asp:Button Text="Validate" ID="Button1" onclick="Button1_Click" runat="server" />

 

     <asp:RangeValidator

        id="rangeValInteger"

        Type="Integer"

        ControlToValidate="txtComp1"

        MaximumValue="100"

        MinimumValue="1"

        runat="server"/>

 

     <asp:RangeValidator

        id="rangeValDate"

        Type="Date"

        ControlToValidate="txtComp2"

        MaximumValue="2003/1/1"

        MinimumValue="2010/1/1"

        runat="server"/>

 

     <asp:RangeValidator

        id="rangeValString"

        Type="String"

        ControlToValidate="txtComp3"

        MaximumValue="Zebra"

        MinimumValue="Aardvark"

        runat="server"/>

     <br>

 

     <asp:Label id="lblOutput" Font-Names="verdana" Font-Size="10pt" runat="server" />

 

    </form>

 

</body>

</html>

Now double click on the Validate(button) and add the following code.

Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        rangeValInteger.Validate()

        If (rangeValInteger.IsValid) Then

            lblOutput1.Text = "Result: Valid!"

        Else

            lblOutput1.Text = "Result: Not Valid!"

        End If

        rangeValDate.Validate()

        If (rangeValDate.IsValid) Then

            lblOutput2.Text = "Result: Valid!"

        Else

            lblOutput2.Text = "Result: Not Valid!"

        End If

 

        rangeValString.Validate()

        If (rangeValString.IsValid) Then

            lblOutput3.Text = "Result: Valid!"

        Else

            lblOutput3.Text = "Result: Not Valid!"

        End If

 

        If (Page.IsValid) Then

            lblOutput.Text = "Result: Page Valid!"

        Else

            lblOutput.Text = "Result: Page Not valid!"

        End If

    End Sub

 

Now save and run the application.


 

rv3.gif
 

Figure 3.


Now enter the integer value, date and string in the above TextBox. and then click on the Button.

rv4.gif
Figure 4.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.