1

We have special requirements for our email verification. We want to allow the following:

  • First and last characters must be alphanumeric.
  • The middle section also allows alphanumeric + the following 3 special characters .-_ (period, hyphen and underscore).
  • Special characters cannot touch (2 periods cannot be next to eachother, or a hyphen underscore for instance).

So far I've managed to figure our how to check for alphanumeric: [^A-Za-z0-9 ]

But I'm completely stuck on the hardest part - the middle section. Thanks.

JasonStockman
  • 532
  • 2
  • 4
  • 9
  • Why are you restricting the users ? Just send an email already. If you really want to validate then [check this answer](http://stackoverflow.com/a/5855853). [This is also a good read](http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/) – HamZa Mar 11 '14 at 06:47
  • Have a look at: http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses – Toto Mar 11 '14 at 08:14

1 Answers1

1

Before you go down the path of email validation with regex, have you tried filter_var() with FILTER_VALIDATE_EMAIL?

if(filter_var($email, FILTER_VALIDATE_EMAIL) !== false)
{
    // Valid email.
}
Marty
  • 39,033
  • 19
  • 93
  • 162