Recipe 54Validating Email Addresses

Task

Suppose you have a form on your website or app that asks the user for an email address. Whether it’s for sending newsletters, regular communication, password recovery, or any other purpose, ensuring that you have the correct email address is crucial before proceeding—to minimize the number of emails returned as undeliverable.

What you need is a mechanism to check that the entered text conforms to a valid email address format.

Solution

If your form is on a website, you can start by setting up the built-in HTML5 form features. First, ensure that your input element has a type attribute with a value of “email”:

 <form>
  <label for=​"Email"​>Email:</label>
  <input type=​ ...

Get Text Processing with JavaScript 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.