This documentation is archived and is not being maintained.

Introduction to the Validation Controls

ASP.NET mobile controls provide a group of controls that applications use to validate the data contained in other controls.

Like other types of controls, you add validation controls to a form by dragging them from the Toolbox in the ASP.NET mobile designer. Validation controls generally do not appear on a form when the user runs the application. Instead, they validate the data in SelectionList and TextBox controls on the same form. Validation occurs when the form is posted to the server. The validation controls test the user's input and, if the data fails any of the validations, the server 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 ControlToValidate property. Because the ValidationSummary control does not reference other controls, it does not contain a ControlToValidate property.

The validation controls provided by ASP.NET mobile controls are based on the ASP.NET Web Forms validation server controls. Like the Web Forms validation controls, the mobile control's validation controls support an IsValid property. This property stores the results of the validation and can be tested programmatically by your application. However, it is not a declarative property (you cannot set it equal to a value). Therefore, it is not shown in the designer's Properties window. For more information on programmatically testing validation results, see Testing Validity Programmatically for Web Server Controls.

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 control, a Panel control, a control's template, or a mobile user control.

Use this control For
CompareValidator Comparing with a Value
CustomValidator Defining Your Own Validation
RangeValidator Checking the Range
RegularExpressionValidator Matching a Pattern
RequiredFieldValidator Requiring an Entry
ValidationSummary Summarizing Errors

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 produces 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 and displayed in one place; for example, 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 Adding and Configuring 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. However, 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 tests that the controls perform are resolved by 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.

Note   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

Overriding Properties | Customizing with Control Templates