Validating an Email Address with Regular Expressions

Back in Chapter 7, one of the tasks was validating an email address. To do the job, the script needed to be relatively long. Script 8.2, at its heart, does exactly the same thing as Script 7.15; but by using regular expressions, it takes many fewer lines, and you get a more rigorous result. You’ll find the simple HTML in Script 8.1, and the CSS is unchanged from Script 7.6.

To validate an email address using regular expressions:

1.
var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
Yow! What on earth is this? Don’t panic; it’s just a regular expression in the validEmail() function. Let’s break it apart and take it piece by piece. Like any line of JavaScript, you read a regular expression ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.