Creating Reusable Server-Side Form Validation Routines
Earlier in this chapter, in Section 5.1.1, we discussed the three common steps taken to create a form:
Create an HTML form with the proper form fields, complete with client-side form validation.
Create a form-processing script and add the needed code.
Add server-side form validation code to the ASP page created in Step 2.
At that time, I mentioned we would look at ways to create robust, reusable code for Steps 1 and 3. Since server-side form validation is paramount to client-side form validation, it makes sense to start by creating code for Step 1. In this section, we will look at how to create a single, reusable ASP page that uses regular expressions to validate form data. In Section 5.5, the discussion will switch to creating a class interface to aid in accomplishing Step 1.
Our reusable server-side form validation routine will exist on the
page. (That means every form on our web site
will have its
ACTION property set to
/CODEREUSE/ValidateForm.asp.) This page will
contain code that will iterate through the proper collection of the
Request object, reading in the value of each form field and
determining whether or not the value is valid.
Our validation page,
/CODEREUSE/ValidateForm.asp, needs to know what
constitutes a valid entry for each and every form field. We’ll
supply this information in the
NAME property of the form field. Since regular expressions are used to validate form field input, each ...