How to validate an e-mail address - JavaScript

/ #JavaScript

Every now and then, we want to check if an e-mail address is valid. Maybe you're building a user sign up or a newsletter registration form?

Validating an e-mail - example

function isValid(email) {
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(String(email).toLowerCase());


If you run this code in your browser, the first e-mail will return true and the second one false.

The code if kind of easy to understand, but the regex not so much.

We simply use a regex to check of the email starts with a certain type of string, if it has a "@", if it has a domain and that the domain has an extension.


No comments yet...

Add comment


Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.