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.

For example, you might add a RangeValidator to check that the number of books purchased is between 1 and 10 (the books are on sale!). To do so, you need only add another validator to the txtNumPurch text box of type RangeValidator. Drag it into the third column and set its properties, as shown in Table 9-7.

Passwords do not match

Figure 9-18. Passwords do not match

Table 9-7. RangeValidator attributes

Property

Value

ID

rangeValid

ControlToValidate

txtNumPurch

Type

Integer

MinimumValue

1

MaximumValue

10

ErrorMessage

Sorry, only 10 per customer

SetFocusOnError

True

The key new attributes are MinimumValue and MaximumValue (setting the range). Set the Type to tell the RangeValidator how to evaluate these values (e.g., as numbers rather than as dates). You can set this from a drop-down list of valid values, as shown in Figure 9-19.

Set the Text property to * to indicate that an asterisk should be displayed if the field is not valid.

Build and run the application again. If you enter a value out of range, an error is displayed, as shown in Figure 9-20.

Figure 9-19. RangeValidation ...

Get Programming Visual Basic 2005 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.