Validating User Input in an ASP.NET Web Page


In ASP.NET input, validation is performed through the use ofyou guessed itWeb controls. The Web controls that perform input validation are commonly called validation Web controls, or just validation controls.

We'll be examining four kinds of validation controls in this hour, which are summarized in Table 12.1. Each of these Web controls is geared for providing one or more of the input validation classes discussed in the preceding sections.

Table 12.1. The ASP.NET Validation Web Controls

Validation Control

Type of Validation

Description

RequiredFieldValidator

Required Field validation

Ensures that data has been entered into a specific input.

CompareValidator

Data type validation and comparison validation

Ensures that a value in one input is less than, less than or equal, equal, greater than, greater than or equal, or not equal to some constant value or some user-inputted value. Can also be used to perform datatype validation.

RangeValidator

Range validation

Ensures that a numeric value in an input is between two constant numeric values.

RegularExpressionValidator

Pattern validation

Ensures that a string value matches some specified pattern.


In the following sections, we will examine each of these validation Web controls in detail. Specifically, we will look at how to add these Web controls to an ASP.NET page, how to specify what user input they are to validate, and how to determine whether the user's input meets the required validation.

After examining each of these four Web controls individually, we will see how to use multiple validation controls on a single ASP.NET page.

An ASP.NET Web Page for Examining the Validation Controls

Before examining how to use these validation controls, let's first create an ASP.NET web page that we can use throughout all of these exercises. Specifically, we will create an ASP.NET page that collects the following information from users:

  • Name, which is a required field

  • Age, which is a numeric field that must be between 0 and 150

  • Social Security number (ssn), which is a string input with the format NNN-NN-NNNN, where N is a digit

  • Number of children, which must be greater than or equal to 0

  • Number of male children, which must be greater than or equal to 0 and less than or equal to the number of total children the person has Start by creating a new ASP.NET page named ValidationControlTestBed.aspx. Now, let's add five TextBox Web controls for the five user inputs. Before each TextBox, enter a descriptive title, such as Your name:, Your age:, and Social-security number:. Figure 12.1 shows the Visual Web Developer after these five TextBox Web controls and label text have been added.

    Figure 12.1. Five TextBox Web controls have been added, along with a title for each.

Now that we've added the needed TextBox Web controls, let's set the ID properties for these TextBox Web controls. Set the first TextBox Web control's ID property to name; the second's to age; the third's to ssn; the fourth's to totalChildren; and the fifth's to maleChildren. For the age, totalChildren, and maleChildren TextBox Web controls, also set the Columns property to 4.

Next, add a Button Web control after the five TextBox Web controls. Set this Web control's ID property to btnSubmit and its Text property to Click Me. Finally, add a Label Web control with ID results below the Button Web control.

Your screen should now look similar to Figure 12.2.

Figure 12.2. The TextBox Web controls have had their properties set, and a Button Web control has been added.


At this point, you may want to test the ValidationControlTestBed.aspx ASP.NET page (you can see this page in Figure 12.7). When you visit the ASP.NET page via a browser, go ahead and enter some test information into the various text boxes. Naturally, there is no input validation, meaning you can enter nonsensical text into any of these text boxes.

By the Way

As we will see shortly, with the ASP.NET validation controls, if you enter data that violates the validation control that is assigned to that particular input, a warning will immediately appear.


Now that we have created the ASP.NET web page to which we will add the various ASP.NET validation controls, we are ready to begin our examination of these controls, starting with the RequiredFieldValidator Web control.




Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

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