5474

I'd like to check if the user input is an email address in JavaScript, before sending it to a server or attempting to send an email to it, to prevent the most basic mistyping. How could I achieve this?

9
  • 23
    @Alex The reason I added this comment is that the suggested regex in the accepted answer will not allow existing live email addresses which is a bad start for a customer, and the really big problem is that even IF the address was accepted it still does not say if it works. The only way to reliably verify that a supplied email is a working valid email is to send a mail with a verification link. So, if your use case does not demand that you verify the email, just do a minimal test for @, otherwise use a verification email. Regex will only provide bad user experience. May 3, 2021 at 14:56
  • @David Mårtensson I added a + on your thoughts. However I do think that a verification email-link thing also can be bad user experience. One that can make you lose a customer.
    – mikael1000
    Jun 3, 2021 at 10:22
  • 7
    @mikael1000 Sure, but what is the purpose of a regex validation when you will not know if its a valid email anyway. If you do not want to intrude on the customer with a validation link just do the most simple validation <something> at <something> and leave it at that. It will ensure that the customer at least added something that might be an email, anything more it mostly a waste of code until you get to actually validating. You could possibly check if the domain exists with a dns lookup. Jun 4, 2021 at 14:23
  • 2
  • 3
    I have to admit, I don't understand why there's some much validation going on when you can't predict if the user made a basic type using the valid characters anyway. Just a check for @ and at least one dot is enough at the start. Aug 23, 2022 at 10:56

79 Answers 79

10

I was looking for a Regex in JS that passes all Email Address test cases:

Here we go :

http://regexr.com/3f07j

OR regex:

Regex = /(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@[*[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+]*/
0
10

In my case, I wanted to avoid ~ and # that's why I have used another solution:

function validEmail(email){
  const regex = /^((?!\.)[\w\-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/;
  return regex.test(email);
}

function validEmail(email){
  const regex = /^((?!\.)[\w\-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/;
  return regex.test(email);
}

const emails = [
'[email protected]',
'[email protected]',
'[email protected]',
'pio_#[email protected]',
'pio_pio@#factory.com',
'[email protected]#om',
'[email protected]*om',
'pio^[email protected]'
]

for(const email of emails){
  document.write(email+' : '+validEmail(email)+'</br>');
}

1
  • This answer - and a few others in this thread - do not escape the hyphen. From what I can see (September 2021) this poses no problem in browsers - at least the Webkit variants. However, this might change. At the time of writing PHP7.3 fails to compile the regex without escaping the hyphen. Future proof your browser side code by escaping the hyphen \- rather than just -.
    – DroidOS
    Sep 29, 2021 at 12:28
9

Most of the answers here are not linter friendly, it's a mess! Some of them are also outdated! After a lot of time spending, I decided to use an external library named email-validator, install it easily by npm for example and import/require it in your own project:

https://www.npmjs.com/package/email-validator

//NodeJs
const validator = require("email-validator");
validator.validate("[email protected]"); // true

//TypeScript/JavaScript
import * as EmailValidator from 'email-validator';
EmailValidator.validate("[email protected]"); // true
1
  • It's not the answer to the question, but it helped me and saved my time. This email-validator package seem to help round about half a million other (downloads) per week. So worth to mention and deserves some upvotes.
    – Stefan
    Oct 20, 2020 at 15:02
8

I prefer to keep it simple and keep my users happy. I also prefer code which is easy to understand. RegEx is not.

function isValidEmail(value) {
    const atLocation = value.lastIndexOf("@");
    const dotLocation = value.lastIndexOf("."); 
    return (
        atLocation > 0 &&
        dotLocation > atLocation + 1 &&
        dotLocation < value.length - 1
    );
};
  • Get the location of the last "@" and the last "."
  • Make sure the "@" is not the first char (there is something before it)
  • Make sure the "." is after the "@" and that there is at least one char between them
  • Make sure there is at least a single char after the "."

Will this allow invalid email addresses to pass? Sure, but I don't think you need much more for a good user experience that allows you to enable/disable a button, display an error message, etc. You only know for sure that an email address is valid when you attempt to send an email to that address.

1
  • This will fail for emails ending in a comment containing a @, for example " [email protected] (this is @ comment)" which is a valid email Feb 16, 2021 at 18:42
7

The regular expression provided by Microsoft within ASP.NET MVC is

/^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/

Which I post here in case it's flawed - though it's always been perfect for my needs.

1
  • 1
    Doesn't allow +'s in the name part of the email. Mar 9, 2015 at 15:19
6

Sectrean's solution works great, but it was failing my linter. So I added some escapes:

function validateEmail(email){ 
     var re = /^(([^<>()[]\\.,;:\s@\"]+(\.[^<>()[]\\.,;:\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 re.test(email); 
}
1
6

The best practice is to either use HTML5 built-in email tag.

<input type="email" name="email">

or the common email syntax as recognizing @ and . from the string is given below.

^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$

Note that this would still produce invalid email that will still match the regex, its almost impossible to catch them all but this will improve the situation a little.

1
  • See earlier comment 32 this is good, but the problem with this is that it must be inside a form tag and submitted by a submit input, which not everyone has the luxury of doing. Also, you can't really style the error message. – @Jason Nov 12 '11 at 0:08
    – aNewb
    Dec 3, 2020 at 2:24
6

This is a JavaScript translation of the validation suggested by the official Rails guide used by thousands of websites:

/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

Relatively simple but tests against most common errors.

Tested on a dataset of thousands of emails and it had zero false negatives/positives.

Example usage:

const emailRegex = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;

emailRegex.test('[email protected]');    // true

// Multi-word domains
emailRegex.test('[email protected]');  // true
emailRegex.test('[email protected]'); // true

// Valid special characters
emailRegex.test('unusual+but+valid+email1900=/!#$%&\'*+-/=?^_`.{|}[email protected]') // true

// Trailing dots
emailRegex.test('[email protected].'); // false

// No domain
emailRegex.test('email@example');        // false

// Leading space
emailRegex.test(' [email protected]');   // false

// Trailing space
emailRegex.test('[email protected] ');   // false

// Incorrect domains
emailRegex.test('email@example,com ');   // false

// Other invalid emails
emailRegex.test('invalid.email.com')        // false
emailRegex.test('invalid@[email protected]') // false
emailRegex.test('[email protected]')       // false
0
5

Here is a function I use for front end email validation. (The Regular Expression came from parsley.js)

<!DOCTYPE html>
<html>
<head>
    <title>Our Company</title>
    <style>
        .form-style {
            color: #ccc;
        }
    </style>
</head>
<body>
    <h1>Email Validation Form Example</h1>
    <input type="text" name="email" id="emailInput" class="form-style">
    <script>
        function validateEmail(emailAddress) {
            var regularExpression = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))){2,6}$/i;
             return regularExpression.test(emailAddress);
        }

        function showEmailValidationState(event) {
            if (validateEmail(event.target.value)) {
                document.getElementById("emailInput").style.color = 'black';
            }
        }
    document.getElementById("emailInput").addEventListener("keyup", showEmailValidationState);
    </script>
</body>
</html>
0
5

The answer to this question (if you stood back and really thought about it) is that you can't really prevent a user from mistyping their e-mail. Everybody is providing answers that match all the possible letters but nobody has really taken into account the full myriad of characters that can be used.

I refer you to this post and the answer explaining the amount of characters that can be accepted:- Remove invalid characters from e-mail

You can't predict if the letters are the exact way the user intended them to be - which is the most common mistake.. missing a letter out or typing the wrong letter.

Ultimately, no matter what you do in Javascript you will always need your backend script to check if the e-mail was sent successfully too and it will probably have a validation process in place too.

So if you wanted to ensure it was some sort of e-mail address and not their username you only really need to check if there's an @ symbol in there and at least 1 dot and leave all the rest to your backend code.

I have provided a very basic function that demonstrates a delayed check for real-time input and an instant check. Javascript is acting as a first checkpoint to validate the basics to save posting invalid content and annoying the user.

It will always assume the bigger check is at the backend though.

// Very simple, non-library dependent client-side  e-mail validator
var emailv = {

  // Timeout handler for checkDelay()
  to: null, 
  
  // The core function that takes a string and validates it
  check : function(em){
    
    // Check 1 - The split ensures there's only one @
    var c1 = em.split('@').length == 2;
    
    // Check 2 - Must be at least 1 dot too
    var c2 = em.indexOf('.') > 0;
    
    // Check 3 - ensures there's always something after a @ or dot
    var c3 = !(em.slice(-1)=="@"||em.slice(-1)==".");
    return (c1&&c2&&c3); // If all TRUE, great.
  },
  
  // Shortcut to quickly check any text input by dom id
  checkById : function(inputId){
    d = document.getElementById(inputId);
    return d?emailv.check(d.value):false;
  },
  
  // Check delay for checking on real-time inputs
  checkDelay: function(em){
      clearTimeout(emailv.to);
      emailv.to = setTimeout("emailv.checkDelayP2('"+em+"')",1000);
  },
  
  // Part two  of Check delay
  checkDelayP2: function(em){ 
      if(emailv.check(em)){ // Javascript filter says it seems okay

        // For sakes of this demo, pretend we are now making a background
        // check to see if e-mail is taken. We tell the user to wait..
        emailv.status("Wait..");

        // Pretend the background check took 2 seconds
        // and said e-mail was available
        setTimeout("emailv.status('OK')",2000);
      } else {
        // Javascript say it's bad, mmmkay?
         emailv.status("BAD E-mail");      
      }
  },
  
  status : function(s){
    document.getElementById('emailstatus').innerHTML=s;
  }
}
<h2>1 of 2 - Delayed check</h2>
<ul>
<li><strong>Waits until the user has stopped typing</strong></li>
<li>Useful for when you want to then send e-mail to background database to check if it exists.</li>
<li>Allows them 1 idle second before checking the e-mail address.</li>
</ul>
<input type="text" size="50" id="youremail" name="youremail" onkeyup="emailv.checkDelay(this.value)" onpaste="emailv.checkDelay(this.value)" />
<span id='emailstatus'></span>


<h2>2 of 2 - Instant Check</h2>
<input type="text" size="50" id="youremail2" name="youremail2" />
<a href="Javascript:void(0)" onclick="alert(emailv.checkById('youremail2')?'Seems okay to me':'This e-mail is dodgy')">Check e-mail</a>

It would be better to avoid stopping the user entering a valid e-mail than to apply so many restrictions that it becomes complicated.

This is just my opinion!

0
4

Following Regex validations:

  • No spacial characters before @
  • (-) and (.) should not be together after @ No special characters after @ 2 characters must before @ Email length should be less 128 characters
function validateEmail(email) {
var chrbeforAt = email.substr(0, email.indexOf('@'));
if (!($.trim(email).length > 127)) {
    if (chrbeforAt.length >= 2) {
        var re = /^(([^<>()[\]{}'^?\\.,!|//#%*-+=&;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
        //var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
        return re.test(email);
    } else {
        return false;
    }
} else {
    return false;
}
}
0
4

Use the regular expression:

 /^[a-z][a-zA-Z0-9_.]*(\.[a-zA-Z][a-zA-Z0-9_.]*)?@[a-z][a-zA-Z-0-9]*\.[a-z]+(\.[a-z]+)?$/

Example:

function validateEmail(email) {
    var re = /^[a-z][a-zA-Z0-9_.]*(\.[a-zA-Z][a-zA-Z0-9_.]*)?@[a-z][a-zA-Z-0-9]*\.[a-z]+(\.[a-z]+)?$/;
    return re.test(email);
}

It should allow only @ , . , _

0
4

You can also try

var string = "[email protected]"
var exp = /(\w(=?@)\w+\.{1}[a-zA-Z]{2,})/i
alert(exp.test(string))
0
4

You can use this regex (from w3resource (*not related to W3C)):

/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailValue)

If you use Node you can use this in the back-end as well as the front-end.

I don't know other back-end languages so I cannot evaluate for other use cases.

3
  • 4
    This regex fails on '[email protected]' and on '[email protected]'. Do not use.
    – Xizam
    Mar 24, 2020 at 11:15
  • user+addition is a valid email and longdomain is not necessarily invalid.. Jan 8, 2021 at 15:30
  • Exactly, no regex will ever be able to fully verify the format of an email and even if it would, it cannot know domain specific rules, just check for an @ and be done with it or send a verification email. Anything else is just asking for trouble. Feb 16, 2021 at 18:30
4

If you get this error: Using regular expressions is security-sensitive.

Then here is what you are looking for. This solution is free from " Regular expression Denial of Service (ReDoS) "

Regex to validate emails without (ReDoS):

/^[a-z0-9](?!.*?[^\na-z0-9]{2})[^\s@]+@[^\s@]+\.[^\s@]+[a-z0-9]$/

Please let me know if this solution works for you. Thanks.

3
<pre>
**The personal_info part contains the following ASCII characters.
1.Uppercase (A-Z) and lowercase (a-z) English letters.
2.Digits (0-9).
3.Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
4.Character . ( period, dot or fullstop) provided that it is not the first or last character and it will not come one after the other.**
</pre>
*Example of valid email id*
<pre>
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
</pre>
<pre>
xxxx.ourearth.com [@ is not present] 
[email protected] [ tld (Top Level domain) can not start with dot "." ]
@you.me.net [ No character before @ ]
[email protected] [ ".b" is not a valid tld ]
[email protected] [ tld can not start with dot "." ]
[email protected] [ an email should not be start with "." ]
xxxxx()*@gmail.com [ here the regular expression only allows character, digit, underscore and dash ]
[email protected] [double dots are not allowed
</pre>
**javascript mail code**

    function ValidateEmail(inputText)
    {
    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if(inputText.value.match(mailformat))
    {
    document.form1.text1.focus();
    return true;
    }
    else
    {
    alert("You have entered an invalid email address!");
    document.form1.text1.focus();
    return false;
    }
    }
1
3

the best one :D (RFC-friendly & no error "too complex") :

function    isMail(mail)
{
    pattuser = /^([A-Z0-9_%+\-!#$&'*\/=?^`{|}~]+\.?)*[A-Z0-9_%+\-!#$&'*\/=?^`{|}~]+$/i;
    pattdomain = /^([A-Z0-9-]+\.?)*[A-Z0-9-]+(\.[A-Z]{2,9})+$/i;

    tab = mail.split("@");
    if (tab.length != 2)
        return false;
    return (pattuser.test(tab[0]) && pattdomain.test(tab[1]));
}
0
3

If you're using Closure you can use the built-in goog.format.EmailAddress type:

http://docs.closure-library.googlecode.com/git/class_goog_format_EmailAddress.html

For example:

goog.format.EmailAddress.isValidAddrSpec("[email protected]")

Note that by reading the source (linked above) you can see the comments state that IDN are not supported and that it only aims to cover most addresses:

// This is a fairly naive implementation, but it covers 99% of use cases.
// For more details, see http://en.wikipedia.org/wiki/Email_address#Syntax
// TODO(mariakhomenko): we should also be handling i18n domain names as per
// http://en.wikipedia.org/wiki/Internationalized_domain_name
3

If you are using ng-pattern and material this does the job.

vm.validateEmail = '([a-zA-Z0-9_.]{1,})((@[a-zA-Z]{2,})[\\\.]([a-zA-Z]{2}|[a-zA-Z]{3}))';
3

Here is the recommended Regex pattern for HTML5 on MDN:

Browsers that support the email input type automatically provide validation to ensure that only text that matches the standard format for Internet e-mail addresses is entered into the input box. Browsers that implement the specification should be using an algorithm equivalent to the following regular expression:

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}
[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email#Validation

0
3

Use the URL interface in JavaScript to parse the address in the minimum practical expected format user@host then check that it looks reasonable. Next send a message to it and see if that works (for example require the recipient validate a one-time token via the address). Note that this handles punycode, internationalization, as shown in the samples below.

https://developer.mozilla.org/en-US/docs/Web/API/URL

an example with simple tests:

function validEmail(input=''){
    const emailPatternInput = /^[^@]{1,64}@[^@]{4,253}$/, emailPatternUrl = /^[^@]{1,64}@[a-z][a-z0-9\.-]{3,252}$/i;
    let email, url, valid = false, error, same = false;
    try{
        email = input.trim();
        // handles punycode, etc using browser's own maintained implementation
        url = new URL('http://'+email);
        let urlderived = `${url.username}@${url.hostname}`;
        same = urlderived === email;
        valid = emailPatternInput.test( email );
        if(!valid) throw new Error('invalid email pattern on input:' + email);
        valid = emailPatternUrl.test( urlderived );
        if(!valid) throw new Error('invalid email pattern on url:' + urlderived);
    }catch(err){
        error = err;
    };
    return {email, url, same, valid, error};
}

[
 'user+this@はじめよう.みんな'
, '[email protected]'
, 'stuff@things'
, '[email protected]'
, 'Jean+Franç[email protected]','هيا@יאללה'
, '试@例子.测试.مثال.آزمایشی'
, 'not@@really'
, 'no'
].forEach(email=>console.log(validEmail(email), email));
2

Following Regex validations:

  • No spacial characters before @
  • (-) and (.) should not be together after @
  • No special characters after @ 2 characters must before @
  • Email length should be less 128 characters

    function validateEmail(email) {
        var chrbeforAt = email.substr(0, email.indexOf('@'));
        if (!($.trim(email).length > 127)) {
            if (chrbeforAt.length >= 2) {
                var re = /^(([^<>()[\]{}'^?\\.,!|//#%*-+=&;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
                return re.test(email);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
2

Whoever is using @pvl solution and wants it to pass ESLint Prefer-template then here's a version where I used template literals instead of string concatenation.

validateEmail(email) {
    let sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
    let sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
    let sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
    let sQuotedPair = '\\x5c[\\x00-\\x7f]';
    let sDomainLiteral = `\\x5b(${sDtext}|${sQuotedPair})*\\x5d`;
    let sQuotedString = `\\x22(${sQtext}|${sQuotedPair})*\\x22`;
    let sDomainRef = sAtom;
    let sSubDomain = `(${sDomainRef}|${sDomainLiteral})`;
    let sWord = `(${sAtom}|${sQuotedString})`;
    let sDomain = `${sSubDomain}(\\x2e${sSubDomain})*`;
    let sLocalPart = `${sWord}(\\x2e${sWord})*`;
    let sAddrSpec = `${sLocalPart}\\x40${sDomain}`; // complete RFC822 email address spec
    let sValidEmail = `^${sAddrSpec}$`; // as whole string

    let reValidEmail = new RegExp(sValidEmail);

    return reValidEmail.test(email);
}
2

In nodeJS you can also use validator node module and simply use like that

Install the library with npm install validator

var validator = require('validator');

validator.isEmail('[email protected]'); //=> true 
2

There are some complex RegEx written here, that also works.

I tested this one and it works too:

[a-zA-Z0-9._]+[@]+[a-zA-Z0-9]+[.]+[a-zA-Z]{2,6}

Please test this here : http://www.regextester.com/?fam=97334

Hope this helps.

1
2

How about creating a function which will test any string against emails' pattern using regular expression in JavaScript, as we know email addresses can be quite different in different regions, like in UK and Australia it usually ends up with .co.uk or .com.au, so I tried to cover those as well, also check if the string passed to the function, something like this:

var isEmail = function(str) {
  return typeof str==='string' && /^[\w+\d+._]+\@[\w+\d+_+]+\.[\w+\d+._]{2,8}$/.test(str);
}

and check if it's email like below:

isEmail('[email protected]'); //true
isEmail('[email protected]'); //true
isEmail('[email protected]'); //true
isEmail('[email protected]'); //true
isEmail('[email protected]'); //true
isEmail('[email protected]'); //true
isEmail('[email protected]'); //true
isEmail('[email protected]#sswzazaaaa'); //false
isEmail('[email protected]'); //false
1
2

ES6 sample

const validateEmail=(email)=> /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\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,}))$/.test(email);
2

Here's a simple regex that would just check for the basic format of an email e.g., [email protected]:

\S+@\S+\.\S+

2

This question is more dificult to answer than seems at first sight.

There were loads of people around the world looking for "the regex to rule them all" but the truth is that there are tones of email providers.

What's the problem? Well, "a_z%@gmail.com cannot exists but it may exists an address like that through another provider "[email protected].

Why? According to the RFC: https://en.wikipedia.org/wiki/Email_address#RFC_specification.

I'll take an excerpt to facilitate the lecture:

The local-part of the email address may use any of these ASCII characters:

- uppercase and lowercase Latin letters A to Z and a to z;
- digits 0 to 9;
- special characters !#$%&'*+-/=?^_`{|}~;
- dot ., provided that it is not the first or last character unless quoted, and provided also that it does not appear consecutively unless quoted (e.g. [email protected] is not allowed but "John..Doe"@example.com is allowed);[6]
Note that some mail servers wildcard local parts, typically the characters following a plus and less often the characters following a minus, so fred+bah@domain and fred+foo@domain might end up in the same inbox as fred+@domain or even as fred@domain. This can be useful for tagging emails for sorting, see below, and for spam control. Braces { and } are also used in that fashion, although less often.
- space and "(),:;<>@[\] characters are allowed with restrictions (they are only allowed inside a quoted string, as described in the paragraph below, and in addition, a backslash or double-quote must be preceded by a backslash);
- comments are allowed with parentheses at either end of the local-part; e.g. john.smith(comment)@example.com and (comment)[email protected] are both equivalent to [email protected].

So, i can own an email address like that:

A__z/J0hn.sm{it!}[email protected]

If you try this address i bet it will fail in all or the major part of regex posted all across the net. But remember this address follows the RFC rules so it's fair valid.

Imagine my frustration at not being able to register anywhere checked with those regex!!

The only one who really can validate an email address is the provider of the email address.

How to deal with, so?

It doesn't matter if a user adds a non-valid e-mail in almost all cases. You can rely on HTML 5 input type="email" that is running near to RFC, little chance to fail. HTML5 input type="email" info: https://www.w3.org/TR/2012/WD-html-markup-20121011/input.email.html

For example, this is an RFC valid email:

"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com

But the html5 validation will tell you that the text before @ must not contain " or () chars for example, which is actually incorrect.

Anyway, you should do this by accepting the email address and sending an email message to that email address, with a code/link the user must visit to confirm validity.

A good practice while doing this is the "enter your e-mail again" input to avoid user typing errors. If this is not enough for you, add a pre-submit modal-window with a title "is this your current e-mail?", then the mail entered by the user inside an h2 tag, you know, to show clearly which e-mail they entered, then a "yes, submit" button.

2

General email regex (RFC 5322 Official Standard): https://emailregex.com/

JavaScript:

/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\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,}))$/

Not the answer you're looking for? Browse other questions tagged or ask your own question.