---
title: "How can I validate an email address in JavaScript?"  
description: "How can I validate an email address in JavaScript?"  
author: "Utpal Vishwas"  
published: 2023-08-29  
updated: 2023-09-02  
canonical: https://www.mindstick.com/forum/159727/how-can-i-validate-an-email-address-in-javascript  
category: "javascript"  
tags: ["javascript events", "html email", "validation"]  
reading_time: 2 minutes  

---

# How can I validate an email address in JavaScript?

How can I [validate an email](https://www.mindstick.com/forum/161200/how-to-validate-an-email-address-in-javascript) address in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Aryan Kumar

There are a few different ways to [validate](https://www.mindstick.com/forum/1490/what-is-better-solution-for-validate-form-textboxs-value) an [email address](https://www.mindstick.com/forum/33871/how-to-validate-email-address-in-javascript) in JavaScript. One way is to use a regular expression. A regular expression is a sequence of characters that can be used to match patterns in text.

The following regular expression can be used to validate an email address:

```plaintext
/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)?[\w-]+)$/
```

This regular expression matches an email address that has the following format:

```plaintext
username@domain.com
```

The `username` part of the email address can contain letters, numbers, and hyphens. The `domain` part of the email address can contain letters, numbers, hyphens, and dots.

Another way to validate an email address in JavaScript is to use a library. There are a number of libraries available that can be used to validate email addresses. One popular library is the email-validator: https://github.com/validatorjs/validator.js/ library.

The following code shows how to use the email-validator library to validate an email address:

```plaintext
import emailValidator from 'email-validator';

const email = 'example@gmail.com';

if (emailValidator.validate(email)) {
  console.log('The email address is valid.');
} else {
  console.log('The email address is invalid.');
}
```

In this code, the `emailValidator` library is imported. The `email` variable is the email address that needs to be validated. The `emailValidator.validate()` method is used to validate the email address. If the email address is valid, the `console.log()` statement will print the message "The email address is valid." If the email address is invalid, the `console.log()` statement will print the message “The email address is invalid.”


---

Original Source: https://www.mindstick.com/forum/159727/how-can-i-validate-an-email-address-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
