ProblemYou need to ensure data entered by a user is within a defined rangefor example, between two numbers, currency values, dates, or alphabetic characters. SolutionAdd 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:
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 outputnormalDiscussionTo 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 messageTo 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();
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 AlsoRecipe 3.1 Example 3-4. Form with range validation (.aspx)
|