-3

I trying to create custom email regexp

[first]@[second].[third]

  • Allowed signs are numbers, letters and some special characters (I have done it)
  • One @
  • First+second to be from 1 to 20 characters long.
  • Email can't start or end with dot. Two or more dots next to each other are also not allowed.
  • Third string to be 2 or 3 characters long.

This is what I have so far:

^(?!\.)(?=[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9]).{2,21}(?:\.[a-zA-Z0-9].{2,3})$

Unfortunately it doesn't work as I expected. Thanks for any tips.

For instance I can add multiple dots next to each other: test...s@fm.com

Knight
  • 551
  • 1
  • 11
  • 24
  • Why do you say it does not work? What inputs did you try and what was the output? – Alfabravo Apr 25 '17 at 21:55
  • well, multiple dots is a valid email value, what's the issue? – zmo Apr 25 '17 at 22:17
  • Possible duplicate of [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) –  Apr 25 '17 at 22:28

2 Answers2

1

Never ever write a regex to check for e-mails. You'll never make it right. As an example you say:

Allowed signs are numbers, letters and some special characters (I have done it)

which is terribly wrong, because you're then not including unicode characters like å or ţ which are valid. I'm also pretty sure you don't know that @ is a valid value within the first part of an e-mail.

Third string to be 2 or 3 characters long.

and what about matching the following tlds: .info, .ninja, .website or .space?

So please, don't.

for reference:

zmo
  • 24,463
  • 4
  • 54
  • 90
  • That's why I've said in title "Custom email". It doesn't have to comply with general standards. Thanks for answer anyway. – Knight Apr 26 '17 at 06:29
0

Following regex might help you.

^(([^<>()\[\]\\.,;:\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,3}))$

Please read answer posted by @zmo too.

Pranav Masariya
  • 53
  • 1
  • 1
  • 4
  • a case for what I said: this regex does not match `foo@example.website` or the multiple dot thing `foo.....bar@example.com` which both are perfectly valid email addresses. – zmo Apr 25 '17 at 22:19