Chapter 9. Validation Controls


Almost every web application requires some kind of user input. The sad fact is, however, that users make mistakes: they skip required fields, they put in six-digit phone numbers, and they return all manner of incorrectly formatted data to your application. Your database routines can choke on corrupted data, and orders can be lost if, for example, a credit card number is entered incorrectly or an address is omitted. So it is imperative that user input be validated, and it is much more efficient to validate the data before it is submitted to the database or to a third-party vendor.

Traditionally, it has taken a great deal of time and effort to validate user input. Each field must be checked and routines must be created for ensuring data integrity. In the event that bad data is found, error messages must be displayed so that the user knows how to correct the problem.

In a given application, you may choose to validate that certain fields have a value, that the values fall within a given range, or that the data is formatted correctly. For example, when processing an order, you may need to ensure that the user has input an address and phone number, that the phone number has the right number of digits (and no letters), and that the social security number entered is in the appropriate form of nine digits separated by hyphens.

Some applications require more complex validation , in which one field is validated to be within a range established by two other fields. For example, you might ask in one field what date the customer wishes to arrive at your hotel, and in a second field you might ask for the departure date. When the user books dinner, you'll want to ensure that the date is between the arrival and departure dates.

There is no limit to the complexity of the validation routines you may need to write. Credit cards have checksums built into their values, as do ISBN numbers. ZIP and postal codes follow complex patterns, as do international phone numbers. You may need to validate passwords, membership numbers, dollar amounts, dates, runway choices, and launch codes.

In addition, it is very desirable for all of this validation to happen client-side so that you avoid the delay of repeated roundtrips to the server while the user tinkers with his input. In the past, this was solved by writing client-side JavaScript to validate the input, and then server-side script to handle input from browsers that don't support client-side programming. Traditionally, this involved writing your validation code twice.

For these reasons, traditional Internet programming requires extensive custom programming for data validation. The ASP.NET 2.0 Framework greatly simplifies this process by providing rich controls for validating user input. They allow you to manage the validation routine very precisely, while requiring far less custom coding. The validation controls also let you specify exactly how and where the error messages will be displayed: either inline with the input controls, aggregated together in a summary report, or both. These controls can be used to validate input for both HTML and ASP controls.

You add validation controls to your ASP document just as you would add any other control. Within the declaration of the validation control, you specify which control is being validated. You may freely combine the various validation controls, and you may write your own, as you'll see later in this chapter.

Sometimes you don't want any validation to occur, such as when a Cancel button is clicked. To allow this, many postback controls, such as Button, ImageButton, LinkButton, ListControl, and TextBox, have a CausesValidation property, which dictates if validation is performed on the page when the control's default event is raised.

If CausesValidation is set to TRue, the default value, the postback will not occur if any control on the page fails validation. If CausesValidation is set to False, however, no validation will occur when that button is used to post the page.

ASP.NET supports the following validation controls:


RequiredFieldValidator control

The simplest validation control, it ensures that the user does not skip over your input control. A RequiredFieldValidator can be tied to a text box to force input into the text box. With selection controls, such as a drop-down menus or radio buttons, the RequiredFieldValidator ensures that the user makes a selection other than the default. The RequiredFieldValidator does not examine the validity of the data, but only makes sure that some data is entered or chosen.


RangeValidator control

Ensures that the value entered is within a specified lower and upper boundary. You can check the range within a pair of numbers (e.g., greater than 10 and less than 100), a pair of characters (e.g., greater than D and less than K) and a pair of dates (e.g., after 1/1/01 and before 2/28/01). The values you check can be constants that you create at design time, or they can be derived from other controls on your page (greater than the value in textBox1 and less than the value in textBox2).


CompareValidator control

Compares the user's entry (greater than, less than, etc.) against another value. It can compare against a constant that you specify at design time, or against another control's property value. It can also compare against a database value.


RegularExpressionValidator control

One of the most powerful validators, it compares the user's entry with a regular expression that you provide. You can use this validator to check for valid social security numbers, phone numbers, passwords, and so forth.


CustomValidator control

If none of these controls meets your needs, you can use the CustomValidator. This checks the user's entry against whatever algorithm you provide in a custom method.

In the remainder of this chapter, we'll examine how to use each of these controls to validate data in ASP.NET applications.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net