Section 8.4. Range Checking

8.4. Range Checking

At times, you'll want to validate that a user 's entry falls within a range. That range can be within a pair of numbers , characters , or dates. In addition, you can express the boundaries for the range by using constants or by comparing its value with values found in other controls.

In this example, you'll prompt the user for a number between 10 and 20 and then validate his answer to ensure it was entered properly. To do so, create a new web site named RangeValidator . Let's do this project entirely in Design mode. To begin, drag four controls onto your page: a label, a text box, a button, and of course, a RangeValidator control, as shown in Figure 8-5.

Figure 8-5. Dragging controls onto a range validator

Click on the label and set its text to "Enter a number between 10 and 20:." Click on the text box, set its ID to txtValue . Click the button and set its text to "Submit." Finally, click on the RangeValidator and in the Properties window click Type . Choose Integer from the drop-down. Set the MinimumValue property to 10 and the MaximumValue property to 20. Next, click on the ControlToValidate property, pick the text box, and set the Text property to "Between 10 and 20 please ."

Run your program. Enter a value and click Submit. The text "Between 10 and 20 please" will be displayed if the value is not within the range of values specified by the MinimumValue and MaximumValue attributes. The Type attribute designates how the value should be evaluated and may be any of the following types: Currency , Date , Double , Integer , or String .

If there are no validation errors, the page can be submitted; otherwise , the range checking error message is displayed.

If you leave the text blank, the validation will pass and the page will be submitted. If you want to ensure some value is entered, you'll need to add a RequiredField validator.



Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

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