One annoying task that most every developer has had to face in the past is form validation. Since forms are an integral part of dynamic, data-driven Web sites, it is essential that a user's query into a form fit the specified guidelines. For example, in a Web site like Amazon.com where users are entering shipping and billing information, it is vital that the user enter credit card numbers, zip codes, email addresses, and other information in an acceptible format.
ASP.NET, which is currently in beta version but is planned on being "officially" released in Q1/Q2 of 2001, greatly
simplifies this annoyance. (To learn more about ASP.NET, be sure to read our ASP.NET
Article Index!) ASP.NET includes six validation controls that you can use to simplify the entire validation
process. ASP.NET also provides a simplified post-back paradigm. In ASP, you can create post-back forms by creating
a form whose
ACTION property in the
FORM tag has the same URL as the page that contains
the form. More simply put, imagine that you had a page named
/scripts/DisplayForm.asp that had the
Note that the
ACTION property is set to
/scripts/DisplayForm.asp, the same URL of the
ASP page displaying the form. Such a technique, referred to as a "post-back form" will cause the page to be reloaded
when the user submits the form.
ASP.NET automatically will pass back the various form field elements through a post-back form. You can also setup
special validator controls whose purpose is to validate a specific form field. If all of the user's entries are
deemed "valid" by these form validators, the
Page.IsValid property is set to
we'll see here shortly in our example.
Of the six ASP.NET validator controls, the three we'll examine in this article are the
RangeValidator, and the
RegularExpressionValidator. As their names imply, the
RequiredFieldValidator should be used on required form fields, the
be used to ensure that the form field value falls within a certain range, and the
ensures that the form field entry corresponds to a regular expression. (To learn more about regular expressions,
be sure to visit our Regular Expressions Article Index!)
You can use any number of validators on a single form field. In our example, we'll ask the user to provide three
pieces of information: their Name, Age, and Social Security Number (SSN). All three fields will have the
RequiredFieldValidator validator; the Age and SSN form fields will have the
furthermore, the Age form field will have a
RangeValidator applied to it as well.
We'll use the following ASP.NET/HTML code to create our three form fields for the Name, Age, and Social Security Number information:
Notice that the
ACTION property of the form is set to
Validation.aspx. Since we want to
make this a post-back form, that means that this code should be placed in a file named:
Notice that we've used both the
asp:TextBox and the
input type="text" server controls.
This is to simply demonstrate that textboxes, in ASP.NET, can be done both ways. Also take note of the
control; this creates the SUBMIT button for the form, and, when clicked, will fire the
subroutine. This sub will be responsible for checking to see if all of the form fields are valid and, if they are,
then taking whatever action the form intends to take!
|Note the |
The only way for an ASP.NET page to realize that you are using a server control (like |
Now that we've created the form fields in our ASP.NET page, we're ready to add the validator controls. We'll look at this task in Part 2!