다음을 통해 공유


Introduction to the Validation Controls 

Validation controls check user input in SelectionList and TextBox controls. Validation occurs when the form is posted to the server. The validation controls test the user's input and, if the input fails any of the validation tests, ASP.NET sends the page back to the client device. When this occurs, the validation controls that detected errors display error messages.

With the exception of the ValidationSummary control, all validation controls must reference a SelectionList or TextBox control. Store the reference in the validation control's ControlToValidate property. Because the ValidationSummary control does not reference other controls, it does not contain a ControlToValidate property.

Types of Validation Controls

Validation controls include five controls that make comparisons and one control that summarizes any errors that occur. All validation controls must be placed within a Form or Panel control, or inside a control's template on a mobile Web page. You can also drag a validation control onto a mobile user control page.

The following table lists the validation controls and links to topics describing the validation tasks they perform.

Use this control Validation task

CompareValidator

Introduction to the CompareValidator Control

CustomValidator

Introduction to the CustomValidator Control

RangeValidator

Introduction to the RangeValidator Control

RegularExpressionValidator

Introduction to the RegularExpressionValidator Control

RequiredFieldValidator

Introduction to the RequiredFieldValidator Control

ValidationSummary

Introduction to the ValidationSummary Control

Displaying Error Information

Validation controls are normally not visible when a form is displayed on a target device. However, if the validation control detects an error, it displays an error message that you specify. The error message can be displayed in a variety of ways:

  • Each validation control can individually display an error message. When it does, it displays the error message at the position of the validation control on the form.

  • Validation errors can be collected in a validation summary control and displayed in one place, such as at the top of the page. This strategy can be used in combination with displaying individual error messages on the form. For more information, see How to: Add and Configure a ValidationSummary Control.

Use the Display property to control whether the validation control displays error messages. The Display property can be set to None, Static, or Dynamic. Select Dynamic to display the results of the validation and None if you do not want to display the results. In the current version of the ASP.NET mobile controls, Dynamic and Static have the same meaning. They both cause the validation control to display its error message if the validation fails.

Validating for Multiple Conditions

Generally, each validation control performs only one test. If you want to check for multiple conditions, you can attach more than one validation control to the TextBox or SelectionList control. In that case, the validation tests are resolved using a logical AND — the data entered by the user must pass all the tests to be considered valid.

In instances where a logical OR operation is required, use the RegularExpressionValidator control. For example, if you are prompting for a phone number, you might allow users to enter a local number, a long-distance number, or an international number. To perform this type of test, use a pattern in the RegularExpressionValidator control.

NoteNote

Applications cannot use multiple validation controls to perform a logical OR operation. Multiple validations on a TextBox or SelectionList control are always performed with a logical AND.

Some of the validation controls test for a valid data type as part of their test. This enables you to combine a test for a data type with a test for a range or specific value into a single validation control. For example, the RangeValidator control can test whether the information that the user entered is a date, and can test that the date falls within a specific range.

See Also

Concepts

Overriding Properties

Other Resources

Customizing with Control Templates