Validating Against a Range of Values for ASP.NET Server Controls
You can check that a user's entry falls within a specific range of values — for example, between two numbers, between two dates, or between alphabetic characters. You set the upper and lower bounds of the range as properties of a RangeValidator control.
You must also specify the data type of the values that the control will validate. If the user's entry cannot be converted to the specified data type — for example, it cannot be converted to a date — the validation fails.
To validate against a range of values
- Add a RangeValidator control to the page and set the following properties:
Property Description ControlToValidate The ID of the control you are validating. ErrorMessage, Text, Display Properties that specify the text and location of the error or errors that will display if the validation fails. For details, see Controlling Validation Error Message Display for ASP.NET Server Controls.
- Set the low and high values of the range using the MinimumValue and MaximumValue properties.
- Set the Type property to specify the data type of the range settings. Use the ValidationDataType enumeration, which allows you to use the type name String, Integer, Double, Date, or Currency.
Note If the user leaves a control blank, the control passes the range validation. To force the user to enter a value, add a RequiredFieldValidator control as well. For details, see Validating Required Entries for ASP.NET Server Controls.
- Add a test in your Web Forms code to check for validity. For details, see Testing Validity Programmatically for ASP.NET Server Controls.