-4

I want an regex for email that starts with numbers and letters.

My regex is

/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/

What is wrong in this? It allows ._- from start and in between. But I don't want this

Valid emails = s@gmail.com, s.p@y.com, s_p123@g.com

Invalid emails = ....s@g.com, s---g@g.com, s...@g.com, 44s..p@g.com, ----s@g.com

Does anyone know how this can be done?

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
  • 1
    Are you aware that TLDs may have much more than 4 character length? See: http://data.iana.org/TLD/tlds-alpha-by-domain.txt – Toto Feb 13 '14 at 09:01
  • 1
    http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/ – Bergi Feb 13 '14 at 09:06

1 Answers1

1

So you dont want to have more than one ._- in your regex? And also it shall not start with ._- Try it this way:

^[a-zA-Z0-9]+[._-]?[a-zA-Z0-9]*@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}

It works for your examples, although im not really sure what email-syntax you really want to test for.

Perhaps you want to use one of many pre written examples in the web (just google ;) )

Mikescher
  • 875
  • 2
  • 16
  • 35