regex - What's the best way to validate an email address in JavaScript?

ID : 54

viewed : 511

Tags : javascriptregexvalidationemail-validationjavascript

Top 5 Answer for regex - What's the best way to validate an email address in JavaScript?

vote vote


Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)

const validateEmail = (email) => {   return String(email)     .toLowerCase()     .match(       /^(([^<>()[\]\\.,;:\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,}))$/     ); }; 

Here's the example of regular expresion that accepts unicode:

const re =   /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; 

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Here's an example of the above in action:

const validateEmail = (email) =>   return email.match(     /^(([^<>()[\]\\.,;:\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,}))$/   ); };  const validate = () => {   const $result = $('#result');   const email = $('#email').val();   $result.text('');    if (validateEmail(email)) {     $result.text(email + ' is valid :)');     $result.css('color', 'green');   } else {     $result.text(email + ' is not valid :(');     $result.css('color', 'red');   }   return false; }  $('#email').on('input', validate); 

And this is the html:

<script src=""></script>  <label for="email">Enter an email address: </label> <input id="email" /> <h2 id="result"></h2> 
vote vote


I've slightly modified Jaymon's answer for people who want really simple validation in the form of:


The regular expression:


To prevent matching multiple @ signs:


Example JavaScript function:

function validateEmail(email)      {         var re = /\S+@\S+\.\S+/;         return re.test(email);     }      console.log(validateEmail('anystring@anystring.anystring'));

vote vote


Just for completeness, here you have another RFC 2822 compliant regex

The official standard is known as RFC 2822. It describes the syntax that valid email addresses must adhere to. You can (but you shouldn'tread on) implement it with this regular expression:


(...) We get a more practical implementation of RFC 2822 if we omit the syntax using double quotes and square brackets. It will still match 99.99% of all email addresses in actual use today.


A further change you could make is to allow any two-letter country code top level domain, and only specific generic top level domains. This regex filters dummy email addresses like asdf@adsf.adsf. You will need to update it as new top-level domains are added.


So even when following official standards, there are still trade-offs to be made. Don't blindly copy regular expressions from online libraries or discussion forums. Always test them on your own data and with your own applications.

Emphasis mine

vote vote


Wow, there are lots of complexity here. If all you want to do is just catch the most obvious syntax errors, I would do something like this:


It usually catches the most obvious errors that the user makes and assures that the form is mostly right, which is what JavaScript validation is all about.

EDIT: We can also check for '.' in the email using

vote vote


There's something you have to understand the second you decide to use a regular expression to validate emails: It's probably not a good idea. Once you have come to terms with that, there are many implementations out there that can get you halfway there, this article sums them up nicely.

In short, however, the only way to be absolutely, positively sure that what the user entered is in fact an email is to actually send an email and see what happens. Other than that it's all just guesses.

Top 3 video Explaining regex - What's the best way to validate an email address in JavaScript?