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?
-
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.– David MårtenssonMay 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.– mikael1000Jun 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.– David MårtenssonJun 4, 2021 at 14:23
-
2Very similar: How can I validate an email address using a regular expression?– Peter MortensenFeb 16, 2022 at 23:18
-
3I 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.– Watts EphersonAug 23, 2022 at 10:56
79 Answers
Wow, there are a lot of answers that contain slightly different regular expressions. I've tried many that I've got different results and a variety of different issues with all of them.
For UI validation, I'm good with the most basic check of looking for an @ sign. It's important to note, that I always do server-side validation with a standard "validate email" that contains a unique link for the user to confirm their email address.
if (email.indexOf('@') > 0)
I have purposely chosen 0 even with zero-based as it also ensures there is a single character before the @.
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 = [
'pio_pio@factory.com',
'~pio_pio@factory.com',
'pio_~pio@factory.com',
'pio_#pio@factory.com',
'pio_pio@#factory.com',
'pio_pio@factory.c#om',
'pio_pio@factory.c*om',
'pio^_pio@factory.com'
]
for(const email of emails){
document.write(email+' : '+validEmail(email)+'</br>');
}
-
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-
.– DroidOSSep 29, 2021 at 12:28
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("test@email.com"); // true
//TypeScript/JavaScript
import * as EmailValidator from 'email-validator';
EmailValidator.validate("test@email.com"); // true
-
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.– StefanOct 20, 2020 at 15:02
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.
-
This will fail for emails ending in a comment containing a @, for example " elldffs@mydomain.kjfdij (this is @ comment)" which is a valid email Feb 16, 2021 at 18:42
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
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);
}
-
validateEmail("e_mail@sth.com") and validateEmail("e.mail@sth.com") both return
false
value– IkromOct 22, 2013 at 10:04
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.
-
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– aNewbDec 3, 2020 at 2:24
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@example.com'); // true
// Multi-word domains
emailRegex.test('email@example.co.uk'); // true
emailRegex.test('email@mail.gmail.com'); // true
// Valid special characters
emailRegex.test('unusual+but+valid+email1900=/!#$%&\'*+-/=?^_`.{|}~@example.com') // true
// Trailing dots
emailRegex.test('email@example.co.uk.'); // false
// No domain
emailRegex.test('email@example'); // false
// Leading space
emailRegex.test(' email@example.com'); // false
// Trailing space
emailRegex.test('email@example.com '); // false
// Incorrect domains
emailRegex.test('email@example,com '); // false
// Other invalid emails
emailRegex.test('invalid.email.com') // false
emailRegex.test('invalid@email@domain.com') // false
emailRegex.test('email@example..com') // false
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>
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;
}
}
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 @ , . , _
You can also try
var string = "hmharsh3@gmail.com"
var exp = /(\w(=?@)\w+\.{1}[a-zA-Z]{2,})/i
alert(exp.test(string))
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.
-
4This regex fails on 'user+addition@gmail.com' and on 'user@email.longdomain'. Do not use.– XizamMar 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
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.
<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>
yoursite@ourearth.com
my.ownsite@ourearth.org
mysite@you.me.net
xxxx@gmail.com
xxxxxx@yahoo.com
</pre>
<pre>
xxxx.ourearth.com [@ is not present]
xxxx@.com.my [ tld (Top Level domain) can not start with dot "." ]
@you.me.net [ No character before @ ]
xxxx123@gmail.b [ ".b" is not a valid tld ]
xxxx@.org.org [ tld can not start with dot "." ]
.xxxx@mysite.org [ an email should not be start with "." ]
xxxxx()*@gmail.com [ here the regular expression only allows character, digit, underscore and dash ]
xxxx..1234@yahoo.com [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;
}
}
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]));
}
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("blah@blah.com")
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
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}))';
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
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!
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; } }
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);
}
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('foo@bar.com'); //=> true
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.
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('alex@example.com'); //true
isEmail('alireza@test.co.uk'); //true
isEmail('peter.example@yahoo.com.au'); //true
isEmail('alex@example.com'); //true
isEmail('peter_123@news.com'); //true
isEmail('hello7___@ca.com.pt'); //true
isEmail('example@example.co'); //true
isEmail('hallo@example.coassjj#sswzazaaaa'); //false
isEmail('hallo2ww22@example....caaaao'); //false
-
What happens with
alireza@test.uk
orphil.h\@\@cked+liked~it@haacked.com
? Feb 1, 2018 at 21:03
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);
Here's a simple regex that would just check for the basic format of an email e.g., X@Y.C
:
\S+@\S+\.\S+
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 "a__z@provider.com.
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. John..Doe@example.com 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)john.smith@example.com are both equivalent to john.smith@example.com.
So, i can own an email address like that:
A__z/J0hn.sm{it!}h_comment@example.com.co
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.
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,}))$/
Use the browser/runtime to handle parsing the input by prepending a protocol and pass it to the URL
API, trapping any errors and check the resulting username
and hostname
properties of the result. It will handle basically all transformations and possibilities (punycode of character sets, etc). This only establishes that the input is parsable, not that is valid--that is only possible through checking if the destination machine receives messages for that alias. This provides a close (imo reasonable) guess though, and can be expanded to be more specific and realistic if you're comfortable both maintaining it and also risking invalid rejections. (Note it doesn't attempt to address IPv4 or IPv6 addresses, simply the broad range of customer-facing scenarios using a domain.)
function validEmail(email=''){
var $0, url, isValid = false, emailPatternInput = /^[^@]{1,64}@[^@]{4,253}$/, emailPatternUrl = /^[^@]{1,64}@[a-z][a-z0-9\.-]{3,252}$/i;
email = email.trim();
try{
url = new URL('http://'+email);
$0 = `${url.username}@${url.hostname}`;
isValid = emailPatternInput.test( email );
if(!isValid) throw 'invalid email pattern on input:' + email;
isValid = emailPatternUrl.test( $0 );
if(!isValid) throw 'invalid email pattern on url:' + $0;
console.log(`email looks legit "${email}" checking url-parts: "${$0 === email ? '-SAME-':$0}"`);
}catch(err){
console.error(`probably not an email address: "${email}"`, err);
};
return isValid;
}
['user+this@はじめよう.みんな', 'stuff@things', 'user+that@host.com', 'Jean+François@anydomain.museum','هيا@יאללה', '试@例子.测试.مثال.آزمایشی', 'not@@really', 'no'].forEach(email=>console.log(validEmail(email), email));
This is the both the simplest and most generally permissive example I can come up with. Please edit it in cases where it can be made to be more accurate while maintain its simplicity and reasonable generally permissive validity.
Also see MDN URL docs URL, window.URL and Nodejs for URL APIs.