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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.