14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 ■ Data Validation Techniques<br />

For example, if you want to test for a US Social Security number, which is a nine-digit number <strong>with</strong> dashes following<br />

the third <strong>and</strong> the fifth digits (for example, 456-33-1234), you can use the following regular expression, <strong>with</strong> optional<br />

dashes (using the ? quantifier), because the user might not enter them:<br />

var searchTerm = /[0-9]{3}\-?[0-9]{2}\-?[0-9]{4}/;<br />

var searchTerm = new RegExp('[0-9]{3}\-?[0-9]{2}\-?[0-9]{4}', ");<br />

Alternatively, you can use the shortcut notation for digits:<br />

var searchTerm = /\d{3}\-?\d{2}\-?\d{4}/;<br />

var searchTerm = new RegExp('\\d{3}\-?\\d{2}\-?\\d{4}', ");<br />

Be aware that if you use the shortcut notations inside quotation marks or in the constructor notation, you need<br />

to precede them <strong>with</strong> double backslashes, not single ones, because you need to escape them! With this knowledge,<br />

you should be well equipped to write your own regular expressions. As proof, let’s go back to the example in the<br />

beginning paragraph of this section:<br />

var validEmail = /^[\w]+(\.[\w]+)*@([\w]+\.)+[a-z]{2,7}$/i<br />

E-mails can be pretty straightforward, like me@example.com, or more complex, like<br />

chris.heilmann.webdev@example.museum. This regular expression should return both as valid e-mails.<br />

It tests whether the string starts <strong>with</strong> a group of one or more word characters, ^[\w]+, followed by a group of 0<br />

or more word characters preceded by a period, (\.[\w]+)*, before the @ sign. After the @ sign, the string might have<br />

one or more groups of one or more word characters followed by a period, ([\w]+\.)+, <strong>and</strong> it’ll end in a string that has<br />

between two <strong>and</strong> seven characters. This last string is the domain, which could be something short like de or longer<br />

like name or museum. Notice that by allowing several words followed by a period, you also make sure that e-mails like<br />

user@open.ac.uk are recognized.<br />

Methods Using Regular Expressions<br />

There are several methods that take regular expressions as parameters. The expression itself—the things inside the<br />

slashes or the RegExp constructor—is called a pattern, because it matches what you want to retrieve or test for.<br />

• instance.test(string): Tests whether the string matches the pattern, <strong>and</strong> returns true<br />

or false.<br />

• instance.exec(string): Matches the string <strong>and</strong> the pattern one time, <strong>and</strong> returns an array of<br />

matches or null.<br />

• instance.match(pattern): Matches the string <strong>and</strong> the pattern, <strong>and</strong> returns the resulting<br />

matches as an array of strings or null.<br />

• instance.search(pattern): Matches the string <strong>and</strong> the pattern, <strong>and</strong> returns the positions of<br />

the positive matches. If the string does not match any of the pattern, the search returns –1.<br />

• instance.replace(pattern, replaceString): Matches the string against the pattern, <strong>and</strong><br />

replaces every positive match <strong>with</strong> replaceString.<br />

• instance.split(pattern, limit): Matches the string against the pattern, <strong>and</strong> splits it into<br />

an array <strong>with</strong> the substrings surrounding the pattern matches as array items. The optional<br />

limit parameter cuts down the number of array elements.<br />

284<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!