Recipe 3.3. Requiring Data to Be in a Range


Problem

You need to ensure data entered by a user is within a defined rangefor example, between two numbers, currency values, dates, or alphabetic characters.

Solution

Add a RangeValidator control to the .aspx file for each TextBox control to be checked, set the minimum and maximum acceptable values for the range, and verify that validation was successful from within the event handler of the control that completes the user's entry for the page.

In the .aspx file:

  1. Add a RangeValidator control for each text box in which the user must enter data within a specified range.

  2. Set the ControlToValidate attribute to the ID of the control to validate.

  3. Set the control's MinimumValue and MaximumValue attributes to the minimum and maximum values for the valid range.

  4. Add Save and Cancel (or equivalently named) buttons.

  5. Set the Save button's CausesValidation attribute to TRue to have validation performed when the button is clicked (set it to False for the Cancel button).

In the code-behind class for the page, use the .NET language of your choice to add code to the event handler for the Save button's click event to check the Page.IsValid property and verify that all validation was successful. (See Recipe 3.1 for details.)

Figure 3-3 shows the user input form introduced in Recipe 3.1 with normal, error-free output. Figure 3-4 shows the same form with the error message that appears on the form when the data entered into the Age field falls outside a predetermined range. Example 3-4 shows the .aspx file that implements the form, and Examples 3-2 and 3-3 (see Recipe 3.1) show the companion code-behind files.

Figure 3-3. Form with range validation outputnormal


Discussion

To make sure a user enters data in a text box within a defined range, place a RangeValidator control on the form and assign it the text box to be validated. To create the form shown in Figures 3-3 and Figures 3-4, for example, we added an asp:RangeValidator control to the .aspx file that implements the form and assigned it to the Age text box to ensure the entered data is within the range 18 to 99. You must place the validator on the form at the exact location where you want the control's error message to be displayed, which in our case is just to the right of the Age text box.

Figure 3-4. Form with range validation outputwith error message


To assign a RangeValidator control to a text box or other control type, you must set its ControlToValidate attribute to the ID of the control you wish to validate. In our example, the ID of the Age text box is txtAge:

 <asp:RangeValidator  Runat="server"  ControlToValidate="txtAge"  Css  Display="Dynamic"  EnableClientScript="True"  MinimumValue="18"  MaximumValue="99"  Type="Integer">    <img src="/books/1/505/1/html/2/images/arrow_alert.gif" alt="arrow"/>    Age Must Be Between 18 and 99 </asp:RangeValidator> 

To specify a valid range, you must set the MinimumValue and MaximumValue attributes of the RangeValidator control. In our example, we have set the lowest acceptable value to 18 and the highest to 99. These values are inclusive, which means that ages 18 and 99 are also acceptable.

 <asp:RangeValidator  Runat="server"  ControlToValidate="txtAge"  Css  Display="Dynamic"  EnableClientScript="True"  MinimumValue="18"  MaximumValue="99"  Type="Integer">    <img src="/books/1/505/1/html/2/images/arrow_alert.gif" alt="arrow"/>    Age Must Be Between 18 and 99     </asp:RangeValidator> 

Our example focuses on using a range of two numbers, but you can use a range of dates, times, currency values, alphabetic characters, etc.

To change the minimum and maximum values dynamically, you can set them in the code-behind, as shown next. You might want to do this, for example, when determining the range on the fly.

 

Dim minAge As Integer Dim maxAge As Integer .. minAge = 18 maxAge = 99 rvAge.MinimumValue = minAge.ToString() rvAge.MaximumValue = maxage.ToString()

int minAge; int maxAge; .. minAge = 18; maxAge = 99; rvAge.MinimumValue = minAge.ToString(); rvAge.MaximumValue = maxAge.ToString();

If control attributes are first set in the .aspx file and later set in the code-behind, the values set in the code-behind will be the values used in the rendered page.


The error message to be output when validation fails is placed between the open and close tags:

 <asp:RangeValidator  Runat="server"  ControlToValidate="txtAge"  Css  Display="Dynamic"  EnableClientScript="True"  MinimumValue="18"  MaximumValue="99"  Type="Integer">   <img src="/books/1/505/1/html/2/images/arrow_alert.gif" alt="arrow"/>   Age Must Be Between 18 and 99 </asp:RangeValidator> 

The error message that will be output when a validation error occurs can be set dynamically in the code-behind, which is something you might find useful when you have set the maximum and minimum range values on the fly:

 

rvAge.Text = "<img src='/books/1/505/1/html/2/images/arrow_alert.gif' alt="arrow"/> " & _ "Age Must Be Between " & minAge.ToString() & _ " and " & maxage.ToString()

rvAge.Text = "<img src='/books/1/505/1/html/2/images/arrow_alert.gif' alt="arrow"/> " + "Age Must Be Between " + minAge.ToString() + " and " + maxAge.ToString();

If you want a text box to be a required field, add a RequiredFieldValidator control to the form as well, which is what we have done in our example with the Age text box:

 <asp:RequiredFieldValidator   Runat="server"  ControlToValidate="txtAge"  Css  Display="Dynamic"  EnableClientScript="True">    <img src="/books/1/505/1/html/2/images/arrow_alert.gif" alt="arrow"/>    Age Is Required </asp:RequiredFieldValidator> <asp:RangeValidator  Runat="server"  ControlToValidate="txtAge"  Css  Display="Dynamic"  EnableClientScript="True"  MinimumValue="18"  MaximumValue="99"  Type="Integer">    <img src="/books/1/505/1/html/2/images/arrow_alert.gif" alt="arrow"/>    Age Must Be Between 18 and 99 </asp:RangeValidator> 

All other aspects of the .aspx and code-behind are the same as for Recipe 3.1. See that recipe's discussion for comments about the Display, EnableClientScript, and CausesValidation attributes in particular. You'll also find explanations of the Save and Cancel buttons and various other aspects of the code.

See Also

Recipe 3.1

Example 3-4. Form with range validation (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master"   AutoEventWireup="false"   CodeFile="CH03RangeValidationVB.aspx.vb"   Inherits="ASPNetCookbook.VBExamples.CH03RangeValidationVB"   title="Range Validation" %> <asp:Content  Runat="server" ContentPlaceHolder>   <div align="center" > Range Validation (VB)   </div>   <table align="center" ></tr>    <td >First Name: </td>    <td>      <asp:TextBox  Runat="server"   Columns="30" Css />    </td>     </tr>     </tr>    <td >Last Name: </td>    <td>  <asp:TextBox  Runat="server"   Columns="30" Css />    </td>     </tr>     </tr>    <td >Age: </td>    <td>  <asp:TextBox  Runat="server"   Columns="30" Css />      <asp:RequiredFieldValidator  Runat="server" ControlToValidate="txtAge" Css Display="Dynamic" EnableClientScript="True"> <img src="/books/1/505/1/html/2/images/arrow_alert.gif" alt="arrow"/> Age Is Required  </asp:RequiredFieldValidator>  <asp:RangeValidator  Runat="server" ControlToValidate="txtAge" Css Display="Dynamic" EnableClientScript="True" MinimumValue="18" MaximumValue="99" Type="Integer">  <img src="/books/1/505/1/html/2/images/arrow_alert.gif" alt="arrow"/>  Age Must Be Between 18 and 99   </asp:RangeValidator>    </td>     </tr>     </tr>    <td >Country: </td>    <td>  <asp:DropDownList  Runat="server" > <asp:ListItem Selected="True" Value="0">----- Select Country -----</asp:ListItem> <asp:ListItem Value="1">Canada</asp:ListItem> <asp:ListItem Value="2">United States</asp:ListItem>      </asp:DropDownList>    </td> </tr>     </tr>    <td >Email Address: </td>    <td>  <asp:TextBox  Runat="server" Columns="30" Css />    </td> </tr> </tr>   <td >Password: </td>   <td>  <asp:TextBox  Runat="server" TextMode="Password" Columns="30" Css />    </td>     </tr>     </tr>   <td >Re-enter Password: </td>   <td>  <asp:TextBox  Runat="server" TextMode="Password" Columns="30" Css />    </td>     </tr>     </tr>   <td colspan="2"> <br/> <table align="center" width="50%"></tr> <td align="center"> <input  runat="server" type="button" value="Save" causesvalidation="true" onserverclick="btnSave_ServerClick"/> </td> <td align="center">      <input  runat="server" type="button"             value="Cancel" causesvalidation="false" onserverclick="btnCancel_ServerClick" />     </td>  </tr>  </table>  </td>   </tr>    </table> </asp:Content> 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

Similar book on Amazon

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