Partager via


How to: Validate Against a Range of Values for ASP.NET Server Controls

You can use the ASP.NET RangeValidator control to determine whether 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

  1. Add a RangeValidator control to the page and set the following properties:

    Property

    Description

    ControlToValidate

    The ID of the control for which the user must provide a value.

    ErrorMessage, Text, Display

    Properties that specify the text and location of the error or errors that will appear if the user skips the control. For details, see How to: Control Validation Error Message Display for ASP.NET Server Controls.

  2. Set the low and high values of the range using the MinimumValue and MaximumValue properties.

  3. Set the Type property to specify the data type of the range settings. Use the ValidationDataType enumeration, which enables you to specify the following types:

    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 How to: Validate Required Entries for ASP.NET Server Controls.

  4. Add a test in your ASP.NET Web page code to check for validity. For details, see How to: Test Validity Programmatically for ASP.NET Server Controls.

See Also

Concepts

Types of Validation for ASP.NET Server Controls

Other Resources

ASP.NET Validation Controls