here's the code that I'm using to validate email address on clicking submit button by the user,
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(EmailID);
I would like to allow apostrophe(') to the email address entered by the user, what would be the modification for the regex above?
-
2See ex-parrot.com/~pdw/Mail-RFC822-Address.html Anything less than that is imperfection. In other words, it's very hard to validate all variations of valid email addresses.Rick Viscomi– Rick Viscomi2013年05月03日 15:00:23 +00:00Commented May 3, 2013 at 15:00
-
1The best way to validate an e-mail address is to try it. As long as it has an @ symbol it could be an e-mail address.nullability– nullability2013年05月03日 15:04:20 +00:00Commented May 3, 2013 at 15:04
-
There is certainly a HUGE gap between the regex offered here and the one Rick Visconni recommends. Your regex is broken in several regards Keethan.symcbean– symcbean2013年05月03日 15:38:44 +00:00Commented May 3, 2013 at 15:38
-
possible duplicate of Use javascript to find email address in a stringsymcbean– symcbean2013年05月03日 15:39:54 +00:00Commented May 3, 2013 at 15:39
-
I recommend using Validator - github.com/chriso/validator.js, email address syntax is impossibly complicated and this library accounts for that.Matthew Rathbone– Matthew Rathbone2018年10月01日 17:57:21 +00:00Commented Oct 1, 2018 at 17:57
3 Answers 3
try this:
/^(\w|')+([\.-]?(\w|')+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
it will allow apostrophe anywhere before the '@'
Comments
Your current Regex match the following email address :
But doesn't match this :
If you're just basically trying to validate an email adress containing one apostrophe like this one :
- test'[email protected]
Then just add a quoi in the first bracket :
/^\w+(['.-]?\w+)@\w+([.-]?\w+)(.\w{2,3})+$/
But it still won't match A LOT of email addresses (one with a dash and an apostrophe, one with multiples dash [...]).
2 Comments
Using Regular Expressions is probably the best way. Here's an example (demo):
function validateEmailAddress(emailID) {
var emailRgx = /^(([^<>()[\]\\.,;:\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 emailRgx .test(emailID);
}