11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<br />

Abstracting Form Validation<br />

<strong>The</strong> previous example illustrated how writing generic input validation routines can be useful.<br />

Instead of having to recode the same or similar field checking functions for each form on your<br />

site, you can write a library of validation functions that can be easily inserted into your pages. In<br />

order to be reusable, such functions should not be hardcoded with form and field names. <strong>The</strong><br />

validation functions should not pull the data to be validated out of the form by name; rather, the<br />

data should be passed into the function for checking. This allows you to drop your functions into<br />

any page and apply them to a form using only a bit of event handler ―glue‖ that passes them the<br />

appropriate fields.<br />

Form checking functions should go beyond checking that fields are non-empty. Common<br />

checks include making sure a field is a number, is a number in some range, is a number of<br />

some form (such as a U.S. ZIP code or Social Security number), is only a range of certain<br />

characters like just alpha characters, and whether input is something that at least looks like an<br />

e-mail address or a credit card number. Many of the checks, particularly the e-mail address and<br />

credit card number checks, are not really robust. Just because an e-mail address looks valid<br />

doesn‘t mean it is. We‘ll present e-mail and numeric checks here as a demonstration of<br />

common validation routines in action.<br />

Note Regular expressions are an invaluable tool for form validation because they let you check<br />

input strings against a pattern using very little code. Without them, you’d be stuck writing<br />

complex string parsing functions manually. We’ll use a combination of manual techniques<br />

and regular expressions. Observe how much easier it is to use regexps.<br />

Many forms are used to collect e-mail addresses, and it is nice to ferret out any problems with<br />

addresses before submission. Unfortunately, it is difficult to guarantee that addresses are even<br />

in a valid form. In general, about the best you can say quickly about an e-mail address is that it<br />

is of the form userid@domain, where userid is a string and domain is a string containing a dot.<br />

<strong>The</strong> ―real‖ rules for what constitutes a valid e-mail address are actually quite complicated, and<br />

take into consideration outdated mail addressing formats, IP addresses, and other corner<br />

cases. Because of the wide variation in e-mail address formats, many validation routines<br />

generally look simply for something of the form string@string. If you want to be extremely<br />

precise, it is even possible not to have a dot (.) on the right side of an e-mail! <strong>The</strong> function here<br />

checks the field passed in to see if it looks like a valid e-mail address.<br />

function isEmail(field)<br />

{<br />

var positionOfAt;<br />

var s = field.value;<br />

if (isEmpty(s))<br />

{<br />

alert("Email may not be empty");<br />

field.focus();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!