Validating Email Addresses

Internet addresses can be tricky things for users—especially new users—to type. You can help them out by scanning the email address they enter and checking it for proper form. For example, you can check that there’s only one @ sign, and that there are no invalid characters, as there are in Figure 7.12. The limit, of course, is that your script can’t catch misspellings, so if the user meant to type in joe@myprovider.com and instead entered joe@yprovider.com, the mistake will go through. Script 7.15 shows you how to snoop through an address for errors.

Figure 7.12. Here’s an example of the kind of entry error that the email validation script will catch.

To validate an email address:

1.
if (allGood && !validEmail(thisTag.value)) ...

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.