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. ...
Get Programming ASP.NET, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.