Categories
JavaScript Answers

How to do email validation with JavaScript regular expression?

Spread the love

Sometimes, we want to do email validation with JavaScript regular expression.

In this article, we’ll look at how to do email validation with JavaScript regular expression.

How to do email validation with JavaScript regular expression?

To do email validation with JavaScript regular expression, we use the /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/ regex.

For instance, we write

const pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;

to assign the regex to pattern.

We use `w+to match any word before the@`.

[a-zA-Z_]+?\.[a-zA-Z]{2,3} matches the hostname part of the email address.

Conclusion

To do email validation with JavaScript regular expression, we use the /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/ regex.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *