RangeValidator Control

I l @ ve RuBoard

RangeValidator Control

The RangeValidator control is used to ensure that the input for a particular form field falls within a particular range of values. It extends upon the BaseValidator class's properties by adding three additional properties described in Table 8.4. To define a range of values for a control, it is necessary to know the minimum and maximum values of the range, as well as the type of data (numeric, date, and so forth) involved.

Table 8.4. RangeValidator Class Additional Properties
Method Type Description
MaximuumValue String The minimum valid value for the ControlToValidate .
MinimumValue String The maximum valid value for the ControlToValidate .
Type ValidationDataType The data type of value in the ControlToValidate . Must be one of the following: Currency , Date , Double , Integer , String .

In execution, the RangeValidator control is fairly simple. It examines the ControlToValidate 's value, and determines whether or not it falls within the range specified by the RangeValidator 's properties. If it does, the vali-dator concludes that the control is valid. Otherwise , the RangeValidator concludes that the ControlToValidate is invalid. One common use of the RangeValidator control is to restrict the range of values one might enter for age. Listing 8.2 demonstrates how you might require users to enter ages between 18 and 65, for example. Figure 8.2 shows the output.

Figure 8.2. Example0802.aspx After Invalid Submit.

Listing 8.2 Adding range validation to a text box.
 <%@ Page language="c#" ClientTarget="DownLevel" %> <script language="C#" runat="server">     void Page_Load()     {                 if(Page.IsPostBack){                     Validate();          if(!IsValid){             if(!age_required.IsValid){                 errorMessage.Text = "You must enter a value for age.<br>";             }             else if(!age_range.IsValid){                 errorMessage.Text = "Age must be between 18 and 65.<br>";             }         }                  }     } </script> <HTML>     <HEAD>         <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >     </HEAD>     <body>         <form method="post" runat="server">             <asp:Label ID="errorMessage" Runat="server"></asp:Label>             <table>                 <tr>                     <td>                         Age:                     </td>                     <td>                         <asp:textbox runat="Server" columns="6"                                                   id="age"></asp:textbox>                     </td>                     <td>                         <asp:requiredfieldvalidator runat="Server"     id="age_required" controltovalidate="age" display="dynamic"      errormessage="Age is required." EnableClientScript="False">                             *                         </asp:requiredfieldvalidator>                         <asp:rangevalidator runat="Server" id="age_range"     controltovalidate="age" display="dynamic"     errormessage="Age must be an integer between 18 and 65." minimumvalue="18"     maximumvalue="65" Type="Integer" EnableClientScript="False">                         *                         </asp:rangevalidator>                     </td>                 </tr>             </table>             <asp:button id="save_button" runat="Server"                            text="Save"></asp:button>         </form>     </body> </HTML> 

In this example, we've demonstrated how it is possible to use more than one validator control on a single form element. In this case, the age TextBox is required and its value must fall between 18 and 65. We've also shown how it is possible to use the Page.IsValid and validation controls' IsValid methods to capture and display detailed error messages in addition to a simple red asterisk (or other marker) next to the offending form element. We will see later in this chapter how another validation control, the ValidationSummary control, does a lot of this work for us and exposes a number of different display options.

One last item to note about Listing 8.2 is the very first line, where we added 'ClientTarget="DownLevel"' . This was done to require the page to submit back to the server on each submission; otherwise the errorMessage control's Text would never have been set on browsers supporting client-side validation, because no invalid page would ever be submitted to the server.

I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net