May 7, 2011, 1:56 p.m.
posted by magedrx
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 Figure. Each of these Web controls is geared for providing one or more of the input validation classes discussed in the preceding sections.
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.
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:
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.
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). 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.
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.