3

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?

5
  • 2
    See 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. Commented May 3, 2013 at 15:00
  • 1
    The 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. Commented 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. Commented May 3, 2013 at 15:38
  • possible duplicate of Use javascript to find email address in a string Commented 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. Commented Oct 1, 2018 at 17:57

3 Answers 3

1

try this:

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

it will allow apostrophe anywhere before the '@'

Sign up to request clarification or add additional context in comments.

Comments

1

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 :

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

Why do you think 'that [email protected]' wont match? if you try the regex, you'll see it will. the ([\.-]?\w+) capture group has a star, so it could be repeated as many times as need be. it won't match this however: '[email protected]'
@hhamilton He actually edited the Regex, it was something else at first
0

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);
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.