How to Validate an Email ID in JavaScript

How to validate an email ID in JavaScript using Node.js with valid and invalid email examples.

Post Date: May 23, 2019
Last Updated: October 26, 2026

Introduction

Email validation is an important part of web application development. It helps ensure that users enter email addresses in the expected format before submitting a form.

JavaScript can be used to validate email addresses on the client side. In Node.js applications, developers can also use validation packages to check whether an email address follows an acceptable format.

In this tutorial, we will use the sane-email-validation package to validate email addresses with the isEmail() and isNotEmail() functions. These examples demonstrate how to check an email address and display the validation result in the console.

Note: Format validation does not confirm that an email inbox exists or can receive messages. Additional verification, such as sending a confirmation email, is needed to establish that an address is reachable.

Prerequisites

Before starting, ensure that you have the following:

  • Basic knowledge of JavaScript.
  • Node.js and npm installed on your system.
  • A code editor or IDE.
  • Access to a terminal or command prompt.

Install the Required Package

Create a project directory if you do not already have one, then open a terminal in that directory.

Install the sane-email-validation package using the following command:

<CodeBlock language="bash" editable> npm install sane-email-validation </CodeBlock>

Once the installation is complete, you can import the package into your Node.js application using require().

Implementation

Method 1: Validate an Email Using isEmail()

The isEmail() function checks whether an email address passes the package’s validation rules. It returns true when the address is considered valid and false otherwise.

Create a JavaScript file named validate-email.js and add the following code:

<CodeBlock language="javascript" editable> var isEmail = require("sane-email-validation");

var email = "yyyyygmail.com";

if (isEmail(email)) {
console.log(email + " is valid.");
} else {
console.log(email + " is not valid.");
}
</CodeBlock>

Code explanation:

  • require(“sane-email-validation”) loads the validation package.
  • The email variable stores the address to be checked.
  • The isEmail() function evaluates the email address.
  • The if-else statement prints the appropriate validation result.

Since the example address is missing the @ symbol, the expected result is

JavaScript email validation output showing an invalid email address checked using the isEmail() function in Node.js.

Method 2: Validate an Email Using isNotEmail()

The package also provides an isNotEmail() function. It performs the inverse check: it returns true when the address fails validation and false when the address passes validation.

Add the following code to your JavaScript file:

<CodeBlock language="javascript" editable> var isNotEmail = require("sane-email-validation").isNotEmail;

var email = "aaaa@gmail.com";

if (isNotEmail(email)) {
console.log(email + " is not valid.");
} else {
console.log(email + " is valid.");
}
</CodeBlock>

Code explanation:

  • The isNotEmail() function is imported from the sane-email-validation package.
  • The email variable stores the address being checked.
  • If isNotEmail() returns true, the program displays an invalid-email message.
  • Otherwise, the program displays a valid email message.

For this example, the expected result is:

Node.js terminal output showing an email address passing validation using the isNotEmail() function from sane-email-validation.

This result indicates that the address passes the package’s format validation rules. It does not confirm that the mailbox actually exists.

Run the JavaScript File

Save the code in validate-email.js and execute it from the project directory:

<CodeBlock language="bash" editable> node validate-email.js </CodeBlock>

The terminal displays the validation result for the email address specified in the script. You can replace the sample address with other test values to observe how the validation functions behave.

Conclusion

Email validation helps applications identify incorrectly formatted email addresses before processing user input. The sane-email-validation package provides the isEmail() and isNotEmail() functions for performing these checks in Node.js applications.

By integrating validation into your application, you can improve input quality and reduce common formatting errors. However, client-side and package-based format validation alone cannot guarantee that an email address exists or is deliverable. For production applications, validate input on the server and use an email confirmation process when address ownership must be verified.

FAQ

1. What is the isEmail() function in JavaScript?

The isEmail() function provided by sane-email-validation checks an email address against the package’s validation rules. It returns true when the address passes and false otherwise.

2. What is the difference between isEmail() and isNotEmail()?

The isEmail() function returns true for an address that passes validation. The isNotEmail() function returns true when the address fails validation, making it the inverse of isEmail().

3. Does JavaScript email validation confirm that an email address exists?

No. Format validation checks the structure of an address but does not establish that the mailbox exists or can receive messages. A confirmation email can help verify that the user has access to the address.

Talk to Our Experts

Need help with JavaScript development, Node.js applications, or input validation? Our team can help you implement practical solutions that meet your application requirements.

Contact our experts to discuss your project.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top