There are a couple of ways to validate email addresses in JavaScript: using regular expressions, synchronous libraries, or APIs. Here’s what you need to know.

There are numerous solutions out there for validating an email address in JavaScript, depending on how strict you want to be with your validation. In this tutorial, you’ll learn about 3 different options and the tradeoffs between them.

Write Your Own Regular Expression

The quick and easy approach is to write a regular expression that validates whether a string is a correctly formatted email address. One simple approach I’ve used in the past is checking if the string looks like xx@yy.zz:


This regular expression is fairly concise and handles many common cases. If you don’t need to be especially strict about validation, this regexp can be helpful.

/^[^@]+@\w+(\.\w+)+\w$/.test(''); // true
/^[^@]+@\w+(\.\w+)+\w$/.test(''); // true
/^[^@]+@\w+(\.\w+)+\w$/.test('foo@bar.c'); // false, TLD must be at least 2 chars
/^[^@]+@\w+(\.\w+)+\w$/.test('foo@bar'); // false
/^[^@]+@\w+(\.\w+)+\w$/.test(''); // false

#javascript #web-development

Email Validation in JavaScript
2.15 GEEK