|MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA|
The regular expression that expresses the format goes something like this:
Again, we want to check to make sure that no forbidden characters have slipped in. For email addresses, we're forbidding the following: ( ) < > [ ] , ; : \ / "
To validate a phone number, first we want to clear out any spacer characters, such as parentheses, dashes, spaces, and dots. We can do this with a regular expression and the
Then we count the length of the number. It should have exactly ten digits -- any more or less, and we reject it.
We want to do a few more kinds of validation. If you present a license or something similar in a text box for the user to accept, you want to make sure that it hasn't been altered when the form is submitted. That's done very simply by comparing the submitted string with the string you were expecting.
Alternately, you can use the
To make sure that a radio button has been chosen from a selection, we run through the array of radio buttons and count the number that have been checked. Rather than sending the whole radio object to a subfunction, which can be problematic (because the radio object has no property indicating which value has been chosen), we pre-process the radio form element in a for loop and send that result to a subfunction for evaluation.
Finally, we want to make sure that the user has selected an option from our drop-down menu (and not the first option, which isn't an option at all, but just a "Choose one" header). For this, we use the select object's
Using The Script
All of the functions are included in a file called validate.js, which can be called with the
Paul Adams is a freelance writer. He regularly contributes to a number of publications on topics including web programming, graphics, technology, and design.
Showing messages 1 through 12 of 12.