{"id":127747,"date":"2022-09-01T17:16:20","date_gmt":"2022-09-01T17:16:20","guid":{"rendered":"https:\/\/phppot.com\/?p=19171"},"modified":"2022-09-01T17:16:20","modified_gmt":"2022-09-01T17:16:20","slug":"javascript-validate-email-using-regular-expression-regex","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/01\/javascript-validate-email-using-regular-expression-regex\/","title":{"rendered":"JavaScript Validate Email using Regular Expression (regex)"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.1489361702128\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on September 1st, 2022.<\/div>\n<p>This tutorial helps to learn how to validate email using regular expression (regex) in JavaScript. It has more than one example of how to do email validation.<\/p>\n<p>Those examples differ in the regex patterns used and in the <a href=\"https:\/\/phppot.com\/php\/php-validate-email\/\">handling of input email<\/a>.<\/p>\n<p>The below quick example uses a regex pattern with a JavaScript <em>match()<\/em> function to validate email. Before finding the match, it converts the input email to lowercase.<\/p>\n<div class=\"post-section-highlight\" readability=\"41\">\n<h2>Quick example<\/h2>\n<pre class=\"prettyprint\"><code class=\"language-javascript\">const validateEmail = (email) =&gt; { return String(email) .toLowerCase() .match( \/^(([^&lt;&gt;()[\\]\\\\.,;:\\s@\"]+(\\.[^&lt;&gt;()[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-z\\-0-9]+\\.)+[a-z]{2,}))$\/ );\n};\n<\/code><\/pre>\n<\/div>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-19283\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/javascript-validate-email-using-regex-550x331.jpg\" alt width=\"550\" height=\"331\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/javascript-validate-email-using-regex-550x331.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/javascript-validate-email-using-regex-300x180.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/javascript-validate-email-using-regex-768x462.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/javascript-validate-email-using-regex.jpg 1000w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<h3>Test results<\/h3>\n<p>When receiving an invalid email format, it returns null. Otherwise, it returns an array of content matched with the regex.<\/p>\n<pre><code>Input: vincy#example\nOutput: null. Input: vincy@example.com\nOutput: vincy@example.co,vincy,vincy,,,example.co,,example.co,example.\n<\/code><\/pre>\n<h2>Simple Email Validation in JavaScript using regex<\/h2>\n<p>This simple email validation script does a basic check with the input email string.<\/p>\n<p>It validates the input email if it has the <a href=\"https:\/\/phppot.com\/jquery\/formatting-date-with-jquery-date-picker\/\">expected format<\/a> regardless of the length and the data type.<\/p>\n<p>I have added this example just to understand how to do pattern-based validation with <a href=\"https:\/\/en.wikipedia.org\/wiki\/Regular_expression\" target=\"_blank\" rel=\"noopener\">regex in JavaScript<\/a>.<\/p>\n<p>I prefer to use the quick example and the strict validation example follows.<\/p>\n<p class=\"code-heading\">simple-validation.html<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php-template\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n&lt;meta charset=\"utf-8\"&gt;\n&lt;title&gt;JavaScript Simple Email Validation using Regular Expression (regex)&lt;\/title&gt;\n&lt;link rel='stylesheet' href='style.css' type='text\/css' \/&gt;\n&lt;link rel='stylesheet' href='form.css' type='text\/css' \/&gt;\n&lt;\/head&gt;\n&lt;body&gt; &lt;div class=\"phppot-container\"&gt; &lt;h1&gt;JavaScript Simple Email Validation using Regular Expression (regex)&lt;\/h1&gt; &lt;div class=\"tile-container\"&gt; &lt;form name=\"form\"&gt; &lt;div class=\"row\"&gt; &lt;label for=\"email\"&gt;Email address: &lt;\/label&gt; &lt;input id=\"email\" name=\"email\" \/&gt; &lt;\/div&gt; &lt;div class=\"row\"&gt; &lt;input type=\"submit\" name=\"submit\" value=\"Submit\" onclick=\"validateEmail(document.form.email)\" \/&gt; &lt;\/div&gt; &lt;\/form&gt; &lt;\/div&gt; &lt;\/div&gt; &lt;script&gt; function matchEmailRegex(emailStr) { var emailRegex = \/\\S+@\\S+\\.\\S+\/; return emailStr.match(emailRegex); }; \/\/ validates in the form anystring@anystring.anystring \/\/ no more fancy validations function validateEmail(emailField) { var emailStr = emailField.value; if (matchEmailRegex(emailStr)) { alert(\"Entered value is a valid email.\"); } else { alert(\"Entered value is not an email.\"); } return false; } &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<h3>Test results<\/h3>\n<pre><code>Input: vincy\nOutput: Entered value is not an email. Input: vincy@example.c\nOutput: Entered value is a valid email.\n<\/code><\/pre>\n<h2>How to do strict email validation in JavaScript<\/h2>\n<p>This example uses an almost similar regex pattern that is used in the quick example. But, it handles the return value of the JavaScript match to <a href=\"https:\/\/phppot.com\/php\/php-login-form\/\">output the validation message<\/a>.<\/p>\n<p>It gives a code to directly include in an application validation script to validate a form email input.<\/p>\n<p>The alert() can be replaced with any form of letting the end user know about the validation status.<\/p>\n<p class=\"code-heading\">strict-validation.html<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php-template\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n&lt;meta charset=\"utf-8\"&gt;\n&lt;title&gt;JavaScript Validate Email using Regular Expression (regex)&lt;\/title&gt;\n&lt;link rel='stylesheet' href='style.css' type='text\/css' \/&gt;\n&lt;link rel='stylesheet' href='form.css' type='text\/css' \/&gt;\n&lt;\/head&gt;\n&lt;body&gt; &lt;div class=\"phppot-container\"&gt; &lt;h1&gt;JavaScript Validate Email using Regular Expression (regex)&lt;\/h1&gt; &lt;div class=\"tile-container\"&gt; &lt;form name=\"form\"&gt; &lt;div class=\"row\"&gt; &lt;label for=\"email\"&gt;Email address: &lt;\/label&gt; &lt;input id=\"email\" name=\"email\" \/&gt; &lt;\/div&gt; &lt;div class=\"row\"&gt; &lt;input type=\"submit\" name=\"submit\" value=\"Submit\" onclick=\"validateEmail(document.form.email)\" \/&gt; &lt;\/div&gt; &lt;\/form&gt; &lt;\/div&gt; &lt;\/div&gt; &lt;script&gt; function matchEmailRegex(emailStr) { var emailRegex = \/^(([^&lt;&gt;()[\\]\\\\.,;:\\s@\\\"]+(\\.[^&lt;&gt;()[\\]\\\\.,;:\\s@\\\"]+)*)|(\\\".+\\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$\/; return emailStr.match(emailRegex); }; function validateEmail(emailField) { var emailStr = emailField.value; if (matchEmailRegex(emailStr)) { alert(\"Entered value is a valid email.\"); } else { alert(\"Entered value is not an email.\"); } return false; } &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<h3>Test results<\/h3>\n<p>Unlike the simple example, it strictly validates the email prefix with the allowed special characters.<\/p>\n<p>It also checks the domain name format to validate the email. The following results show the test cases and respective outputs of the JavaScript email validation.<\/p>\n<pre><code>Input: vincy\nOutput: Entered value is not an email. Input: vincy@example.c\nOutput: Entered value is not an email. Input: vincy@example.com\nOutput: Entered value is a valid email.\n<\/code><\/pre>\n<p><a class=\"download\" href=\"https:\/\/phppot.com\/downloads\/javascript\/javascript-validate-email-regex.zip\">Download<\/a><\/p>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/javascript\/javascript-validate-email-regex\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on September 1st, 2022. This tutorial helps to learn how to validate email using regular expression (regex) in JavaScript. It has more than one example of how to do email validation. Those examples differ in the regex patterns used and in the handling of input email. The below quick example uses [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":127748,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-127747","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127747"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127747\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/127748"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}