-2

I have been searching around a bit for a good regex for email validation, but for most of the ones I am finding I see the people comment saying the regex is outdated, or it doesn't work... so I am hoping that someone can help me out with an email validation regex that is currently valid for all emails...

here's what I have so far : I have seen people saying that emailReg2 is outdated and produces false positives, but haven't seen anything about emailReg1 being outdated.

var emailReg1 = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;
var emailReg2 = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;

function IsEmail(email) {
  var regex = //which regex do I put here?
  return regex.test(email);
}

any clarification is greatly appreciated. Thanks!

Adjit
  • 10,134
  • 12
  • 53
  • 98
  • @PedroLobito I did... someone edited the question to say that I didn't search... that is actually a bit frustrating... I remember I specifically said in the beginning `I have been searching for some time...` – Adjit May 08 '14 at 14:43
  • I rolled back your question, it still doesn't explain why this question isn't like yours http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – Pedro Lobito May 08 '14 at 14:45
  • @PedroLobito its a 5 yr old question and the first comment in the answer is : `This regex eliminates valid, in-use emails. Do not use. Google for "RFC822" or "RFC2822" to get a proper regex.` – Adjit May 08 '14 at 14:47
  • 1
    the answers are just as valid. not to mention that email addresses haven't really changed much. – wei2912 May 08 '14 at 15:19
  • @Adjit also note that "bogus@nonexistant.com" is a valid email address. – Pointy May 08 '14 at 15:29
  • @Pointy right, all I really want to make sure is that there is nothing harmful in the email address – Adjit May 08 '14 at 15:31
  • Do these also validate European addresses as well? Like eva.lichtenberger@europarl.europa.eu ? –  Dec 11 '14 at 02:23

3 Answers3

0
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);
}
Prosto Trader
  • 3,471
  • 3
  • 31
  • 52
0

For a regex to validate email addresses, you should look at your needs and determine if you do need something complex or something very simple.

For a simple (and hackish) regex:

.+?@.+?\..+

Going indepth:

.+?   # At least 1 or more character, non-greedy; matches everything before the @ sign
@     # literal @
.+?   # Same as above; matches everything before the .
\.    # literal .
.+    # match everything after

This doesn't work for domains like appname.appspotmail.com as it has multiple fullstops in it, but should suit your needs.

You can modify the above regex to accept such conditions. As mentioned, it all depends on your needs. There is no better or worse regex for email validation; there is only a regex that meets your needs perfectly and a regex that is unnecessarily verbose.

If possible, you should come up with your own regex instead of using others, as you're the only person who will know your own needs.

If you must use something that complies with the RFC, look at this extremely verbose regex: http://ex-parrot.com/~pdw/Mail-RFC822-Address.html However, I would discourage you from using that regex and instead look for a library that validates email addresses. You probably don't need to comply with the RFC, though.

If you want resources for debugging regular expressions, give http://regex101.com a try.

wei2912
  • 6,141
  • 2
  • 19
  • 20
  • so would I be able to make a more complex regex... where if it fails the first time, try a different regex to check for multiple fullstops? so the next regex would be something like `.+?@.+?\..+?.+` – Adjit May 08 '14 at 14:50
  • you would use closures. pick up a guide on regular expressions; http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124 is one of the best IMO – wei2912 May 08 '14 at 14:51
-1

The REGEX I use for Email validation is

^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$

There are a number of REGEX questions on SO for the Best Email Validation.

Best Regular Expression for Email Format Validation with ASP.NET 3.5 Validation

Validate email address in JavaScript?

Using a regular expression to validate an email address

From most of my research you will find plenty of REGEX that will validate most email addresses, but I have never found one that will catch all emails. You have to find the one that fits your needs the best.

Community
  • 1
  • 1
Steve P
  • 162
  • 1
  • 11