{"id":4705,"date":"2019-05-23T10:38:24","date_gmt":"2019-05-23T05:08:24","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4705"},"modified":"2026-10-27T13:49:19","modified_gmt":"2026-10-27T08:19:19","slug":"how-to-validate-email-id-in-javascript","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-validate-email-id-in-javascript\/","title":{"rendered":"How to Validate an Email ID in JavaScript"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Post Date: May 23, 2019<br>Last Updated: October 26, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before starting, ensure that you have the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Basic knowledge of JavaScript.<\/li>\n\n\n\n<li>Node.js and npm installed on your system.<\/li>\n\n\n\n<li>A code editor or IDE.<\/li>\n\n\n\n<li>Access to a terminal or command prompt.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Install the Required Package<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a project directory if you do not already have one, then open a terminal in that directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Install the sane-email-validation package using the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;CodeBlock language=\"bash\" editable> npm install sane-email-validation &lt;\/CodeBlock><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once the installation is complete, you can import the package into your Node.js application using require().<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Method 1: Validate an Email Using isEmail()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The isEmail() function checks whether an email address passes the package&#8217;s validation rules. It returns true when the address is considered valid and false otherwise.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a JavaScript file named validate-email.js and add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;CodeBlock language=\"javascript\" editable> var isEmail = require(\"sane-email-validation\");\n\nvar email = \"yyyyygmail.com\";\n\nif (isEmail(email)) {\nconsole.log(email + \" is valid.\");\n} else {\nconsole.log(email + \" is not valid.\");\n}\n&lt;\/CodeBlock><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>require(&#8220;sane-email-validation&#8221;) loads the validation package.<\/li>\n\n\n\n<li>The email variable stores the address to be checked.<\/li>\n\n\n\n<li>The isEmail() function evaluates the email address.<\/li>\n\n\n\n<li>The if-else statement prints the appropriate validation result.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Since the example address is missing the @ symbol, the expected result is<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2026-10-27-133044.png\"><img decoding=\"async\" width=\"750\" height=\"48\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2026-10-27-133044.png\" alt=\"JavaScript email validation output showing an invalid email address checked using the isEmail() function in Node.js.\" class=\"wp-image-12510\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2026-10-27-133044.png 750w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2026-10-27-133044-300x19.png 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Method 2: Validate an Email Using isNotEmail()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code to your JavaScript file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;CodeBlock language=\"javascript\" editable> var isNotEmail = require(\"sane-email-validation\").isNotEmail;\n\nvar email = \"aaaa@gmail.com\";\n\nif (isNotEmail(email)) {\nconsole.log(email + \" is not valid.\");\n} else {\nconsole.log(email + \" is valid.\");\n}\n&lt;\/CodeBlock><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The isNotEmail() function is imported from the sane-email-validation package.<\/li>\n\n\n\n<li>The email variable stores the address being checked.<\/li>\n\n\n\n<li>If isNotEmail() returns true, the program displays an invalid-email message.<\/li>\n\n\n\n<li>Otherwise, the program displays a valid email message.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For this example, the expected result is:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2026-10-27-134103.png\"><img decoding=\"async\" width=\"750\" height=\"33\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2026-10-27-134103.png\" alt=\"Node.js terminal output showing an email address passing validation using the isNotEmail() function from sane-email-validation.\" class=\"wp-image-12511\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2026-10-27-134103.png 750w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2026-10-27-134103-300x13.png 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This result indicates that the address passes the package&#8217;s format validation rules. It does not confirm that the mailbox actually exists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Run the JavaScript File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Save the code in validate-email.js and execute it from the project directory:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;CodeBlock language=\"bash\" editable> node validate-email.js &lt;\/CodeBlock><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">1. What is the isEmail() function in JavaScript?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The isEmail() function provided by sane-email-validation checks an email address against the package&#8217;s validation rules. It returns true when the address passes and false otherwise.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. What is the difference between isEmail() and isNotEmail()?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">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().<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Does JavaScript email validation confirm that an email address exists?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Links<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/dpkg-error-processing-package-javascript-common\/\">Resolving the dpkg Error: Error Processing Package javascript-common<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Official Links<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/scottgonzalez\/sane-email-validation\" target=\"_blank\" rel=\"noopener\">sane-email-validation GitHub Repository<\/a> \u2014 Installation instructions and usage examples.<\/li>\n\n\n\n<li><a href=\"https:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noopener\">Node.js Official Website<\/a> \u2014 Download Node.js and npm.<\/li>\n\n\n\n<li><a href=\"https:\/\/docs.npmjs.com\/\" target=\"_blank\" rel=\"noopener\">npm Official Documentation<\/a> \u2014 Learn how to install and manage Node.js packages.<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\" target=\"_blank\" rel=\"noopener\">MDN JavaScript Guide<\/a> \u2014 JavaScript language fundamentals.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to Our Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Need help with JavaScript development, Node.js applications, or input validation? Our team can help you implement practical solutions that meet your application requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/contact\">Contact our experts<\/a> to discuss your project.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: May 23, 2019Last Updated: October 26, 2026 Introduction Email validation is an important part of web application development. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12512,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[660,661,337],"class_list":["post-4705","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-architecture","tag-emailid-validation","tag-javascript","tag-nodejs","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1dT","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/How-to-Validate-Email-in-JavaScript.png","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4705"}],"version-history":[{"count":2,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4705\/revisions"}],"predecessor-version":[{"id":12513,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4705\/revisions\/12513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media\/12512"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}