Checking a Range
The RangeValidator control allows you to verify that the value of a control
falls within a specified range. For example, you could make sure that a
text box's value is between 5 and 10 or between a month ago and the current
date.
Normally, you'll need to set the properties shown in Table 8.3, in addition
to those listed in Table 8.1.
Table 8.3. Set These Properties for RangeValidator Controls Property
Description
MaximumValue The maximum allowable value.
MinimumValue The minimum allowable value.
Type The type of data to compare, selected from String, Integer, Date,
Double, or Currency. This property controls the type of comparison to
perform梚t wouldn't make sense to perform a text comparison when you're
entering numeric values, for example. If you did, "2" would be greater than
"11." Select the correct data type to perform the correct type of
comparison.
In order to try out this control, you will modify the sample page,
EmpMaint.aspx, adding a RangeValidator control to ensure that the employee
birth date falls between 1/1/1900 and 12/31/1984. Here are the steps to
follow:
Click to the right of the RequiredFieldValidator control that's associated
with the BirthDate field, moving the selection point to that location.
In the Toolbox, double-click the RangeValidator control to add it adjacent
to the existing RequiredFieldValidator control.
Set the properties for the control as shown in Table 8.4.
Table 8.4. Set These Properties for the RangeValidation Control Property
Value
ID rvalBirthdate
ControlToValidate txtBirthDate
ErrorMessage Birth Date must be between 1/1/1900 and 12/31/1984
MaximumValue 12/31/1984
MinimumValue 1/1/1900
Type Date
Right-click the page and select Build and Browse from the context menu.
Enter an invalid date for the Birth Date field (perhaps a date after
12/31/1984) and then press Tab to leave the control. You should see an
error appear immediately.
Close the browser window.
TIP
Although we've used dates formatted correctly for the United States here,
you'll need to take into account the current locale when testing your
pages. If your date format is different from ours, make sure you attempt to
validate dates that are correct for the locale of the browser.
Although you did see the correct error message, you may have noticed that
it didn't appear immediately to the right of the associated text box.
Because you had already placed another validation control adjacent to the
Birth Date text box, that control "consumes" the space where you'd expect
to see the out-of-range error message.
To solve this problem, you can use the Display property of the validation
controls. This property has three values: None, Dynamic, and Static. The
default value is Static, indicating that the control always takes up the
same amount of space on the rendered page. Selecting None causes the
control to take up no space and never display any text (unless you add a
ValidationSummary control to the page). Selecting Dynamic allows the
control to take up no space if it isn't displaying any text. This choice,
then, will fix the problem. Simply follow these steps:
Select each of the validation controls on the page and set the Display
property for each to Dynamic.
Repeat the previous experiment and prove to yourself that the display works
better now.
TIP
If you only want to validate that the user has entered a particular data
type (a date, for example) but don't care about the specific value, you can
use the CompareValidator control, discussed later in the chapter.
If you want to provide a dynamic range for the RangeValidator control (for
example, allowing dates to be entered for the Birth Date field between 70
years ago and 17 years ago, figuring that you can't hire anyone older than
70 or younger than 17), you'll need to write a little code. You might want
to modify the EmpMaint.aspx page so that it adds these restrictions as it
loads.
Modify the Page_Load procedure of EmpMaint.aspx so that it looks like this:
Private Sub Page_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
With rvalBirthDate
.MinimumValue = CStr(Today.AddYears(-70))
.MaximumValue = CStr(Today.AddYears(-17))
.ErrorMessage = String.Format( _
"Enter a date between {0} and {1}", _
.MinimumValue, .MaximumValue)
End With
End If
End Sub
Browse the page again and try entering a birth date that's more than 70
years ago or less than 17 years ago. You'll see the error message,
indicating the valid range of dates.
The previous example brings up some interesting ideas:
You can use the AddYears method of a date to add or subtract years.
The MinimumValue and MaximumValue properties of the RangeValidator control
both accept String values梱ou'll need to convert anything you place in
these properties into strings.
You can dynamically modify the ErrorMessage text of a control.