asp.net.ph

Skip Navigation Links

Introduction to Validating User Input in Web Forms

ASP.NET Web Forms   Web Forms Server Controls   Web Forms Validation


An important aspect of creating Web Forms pages for user input is to be able to check that the information users enter is valid. The Web Forms framework provides a set of validation controls that provide an easy-to-use but powerful way to check for errors, and if necessary, display messages to the user.

You can add input validation to Web Forms by using validation controls. Validation controls provide an easy-to-use mechanism for all common types of standard validation — for example, testing for valid dates or values within a range — plus ways to provide custom-written validation. In addition, validation controls allow you to completely customize how error information is displayed to the user.

Validation controls can be used with any controls that are processed in a Web Form’s class file, including both HTML and ASP.NET server controls.

Using Validation Controls

You enable validation of user input by adding validation controls to your form as you would other server controls. There are controls for different types of validation, such as range checking or pattern matching. A complete list appears in the topic Types of Validation.

Each validation control references an input control ( a server control ) elsewhere on the page. When the user’s input is being processed ( for example, when the form is submitted ), the page framework passes the user’s entry to the appropriate validation control or controls. The validation controls test the user’s input and set a property to indicate whether the entry passed the test. After all validation controls have been called, a property on the page is set; if any of the controls shows that a validation check failed, the entire page is set to invalid.

You can test the state of the page and of individual controls in your own code. For example, you would test the state of the validation controls before updating a data record with information entered by the user. If you detect an invalid state, you bypass the update. Typically, if any validation checks fail, you skip all your own processing and the page is returned to the user. Validation controls that detected errors then produce an error message that appears on the page.

Validating for Multiple Conditions

Generally speaking, each validation control performs just one test. However, frequently you want to check for multiple conditions. For example, you might want to establish that a field is required and that it is limited to accepting dates within a specific range.

You can attach more than one validation control to an input field on a form. In that case, the tests performed by the controls are resolved using a logical AND: the data input by the user must pass all the tests in order to be considered valid.

NOTE: Several of the validation controls test for a valid data type as part of their test. This allows you to combine a test for a data type with a range or specific value in a single validation control. For example, using validation control for ranges, you can test both that the user has entered a date as well as testing that the date falls within a specific range.

In some instance, several different entries might be valid. 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. This situation arises primarily when checking for specific patterns of numbers or characters. To perform this type of test — a logical OR — you use the pattern-matching validation control and specify multiple valid patterns within the control. ( Using multiple validation controls does not work because they are tested with a logical AND ) .

Displaying Error Information

Validation controls are normally not visible in the rendered form. However, if the control detects an error, it produces error message text that you specify. The error message can be displayed in a variety of ways, as listed in the following table.

Display option Description
In place Each validation control can individually display an error message or glyph in place ( usually next to the control where the error occurred ) .
Summary Validation errors can be collected and displayed in one place, for example, at the top of the page. ( This strategy is often used in combination with displaying a glyph next to the input fields with errors. ) If the user is working in a browser that supports DHTML, the summary can be displayed in a pop-up message box.
Custom You can create custom error display by capturing the error information and designing your own output.

If you use the in-place or summary display options, you can format the error message text using HTML.

Server-Side and Client-Side Validation

Validation controls perform input checking in server code. When the user submits a form to the server, the validation controls are invoked to review the user’s input, control by control. If an error has occurred in any of the input controls, the page itself is set to an invalid state so you can test for validity before your code runs.

If the user is working with a browser that supports DHTML, the validation controls can also perform validation using client script. This can substantially improve response time in the page; errors are detected immediately and error messages are displayed as soon as the user leaves the control containing the error. If client-side validation is available, you have greater control over the layout of error messages and can display an error summary in a pop-up message box. For more details, see Client-Side Validation.

The page framework performs validation on the server even if the validation controls have already performed it on the client, so that you can test for validity within your server-based event-handling methods. In addition, it helps prevent users from being able to bypass validation by impersonating another user or a pre-approved transaction.

Validation Object Model

You can interact with validation controls using the object model exposed by individual validation controls and by the page. Each validation control exposes its own IsValid property that you can test to see if a validation test has passed or failed for that control.

At the page level you have access to another IsValid page property that summarizes the IsValid state of all the validation controls on the page. This property makes it easy to do a quick test and see if you can safely proceed with your own processing.

The page also exposes a Validators collection containing a list of all the validation controls on the page. You can loop through this collection to examine the state of individual validation controls.

NOTE: There are a few differences in the object model for client-side validation. For details, see Client-Side Validation.

Custom Validation

You can customize the validation process in a number of ways:

  • You have complete control over the format, text, and location of error messages, and can specify whether they appear individually or in summary form.
  • You can create custom validation using a special control. The control calls your logic but otherwise works the same as other validation controls in setting error state, displaying error messages, and so on. This provides an easy way to create custom validation logic but still participate in the validation framework of the page.
  • If you have specified client-side validation, you can intercept the validation call and substitute or add your own validation logic.

Validating Programmatically

By default, validation controls perform their checks when a form is submitted. However, you can perform validation at any time in your code by calling a validation control’s Validate method. Calling this method allows you to evaluate the validity of property settings made programmatically. For example, you might be gathering data from a user using a method other than a Web Form page, and then setting properties of server controls programmatically.

For more details, see Validating Programmatically.

See Also

Data Entry Validation ( Client-Side )



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note