-1

I have a regex for my email validation which is:

^(?!.*\.{2})[a-zA-Z0-9_\.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+

I need to be able to prevent the user from entering the below email addresses :

.email@example.com

email.@example.com

email@example.com (Joe Smith)

email@-example.com

email@111.222.333.44444

How do I modify my existing regex so that it handles the above email addresses?

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
Debasish
  • 417
  • 1
  • 10
  • 21
  • That's not email validation, you're presenting email restriction. – MonkeyZeus Feb 05 '20 at 16:18
  • Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Feb 05 '20 at 16:35

2 Answers2

0

Simplest would be just to add them conditions into your RegEx

^[^\.][a-zA-Z0-9_\.+-]+[^\.]@[^-][a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+$

https://regex101.com/r/dD8azG/1

Kevin Smith
  • 13,746
  • 4
  • 52
  • 77
  • This is matching `§............#@?------------..............` not sure it's a valid email. – Toto Feb 05 '20 at 16:35
  • fit to their condition of solving their criteria, I assumed they didn't want an out the box regex for email addresses? – Kevin Smith Feb 05 '20 at 16:36
  • For example, they don't want to match 2 consecutive dots, your regex does. see: https://regex101.com/r/dD8azG/2 – Toto Feb 05 '20 at 16:39
0

This regex seems to be working fine.

^(?!.*.{2})[^<>()[]\,.;:\%#^\s@\"$&!@]+@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z0-9]+.)+[a-zA-Z]{2,}))$

Debasish
  • 417
  • 1
  • 10
  • 21