0

I've been trying to create a reg ex that supports gmail's "plus":

name+thing@gmail.com

^[\w-.]+@([\w-]+.)+[\w-]{2,4}$

How do I allow the + ?

pts
  • 80,836
  • 20
  • 110
  • 183
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • 8
    There is [*no easy way*](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) to validate an email address [*using regex*](http://www.regular-expressions.info/email.html) – Nir Alfasi Jul 06 '14 at 22:11
  • 2
    ^[\w-.\+]+@([\w-]+.)+[\w-]{2,4}$ – Valijon Jul 06 '14 at 22:15
  • 1
    @alfasin is right. There's no easy way. However, if you really want to try, take look at this blog post by Phil Haack: [I Knew How to Validate an Email Address Until I Read the RFC](http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/). – fourpastmidnight Jul 06 '14 at 23:20
  • 2
    [tag:regex] is a metatag. Please also tag your question with the language you're using, as regex features vary from implementation to implementation. – ClickRick Jul 06 '14 at 23:55

2 Answers2

1

As others said, it's so hard to validate an email-address exactly. But we could do a low level validation. For validation, you don't need to capture anything. So turn the capturing group in your regex to non-capturing group.

^[\w-.+]+@(?:[\w-]+.)+[\w-]{2,4}$

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

There is only one truly reliable way to store only real email addresses in your database: send a confirmation mail with a confirmation link. vladimir.poetin@whitehouse.gov will pass all checks, but is very probably not a real address.

To catch typos by the user, and thus prevent unnecessary action by your server, just have him/her enter the address twice, and compare the inputs.