Web Development - A Quick Little JavaScript Email Validation Routine Using Regular Expressions

There are lots of ways to verify that you have a valid email address. JavaScript's regular expressions capability provides one of the most concise methods. Here's a quick little function you can use to verify that the argument sEmail follows the standard email format:

  • function isEmail(sEmail) { var regEmail = /^([w-]+.?)*w+@([da-zA-z-]+.)+[a-zA-z]{2,6}$/; return regEmail.test(sEmail); }

Note that this only checks the length of the extension, not whether it's an actual top-level domain. Currently, the extensions for top-level domains consist of anywhere from two letters (for country codes) to six letters, such as .museum. If you want to further and verify the top-level domain (perhaps in your server-side code), you'll need to get a hold of the official list. You can get all the current domain suffixes and information on organizations that register and maintain these names from the Internet Assigned Numbers Authority (IANA) site at http://www.iana.org/

Go back