Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

E-mail validation with preg_match

New Here ,
Jul 28, 2008 Jul 28, 2008

Copy link to clipboard

Copied

Hi,

I've picked up a simple e-mail vaildation string from one of the forums which seems to work (most times)...

I've just hit a problem where it is rejecting an e-mail address and I can't see why.

The vailidation statment is....

if (preg_match("/^([a-z0-9]([a-z0-9_-]*\.?[a-z0-9])*)(\+[a-z0-9]+)?@([a-z0-9]([a-z0-9-]*[a-z0-9])*\.)*([a-z0-9]([a-z0-9-]*[a-z0-9]+)*)\.[a-z]{2,6}$/", $address))
{ return true; }
else
{ return false; }

.. but it doesn't like the bit after the '@' sign to be more than 16 characters long in total which I don't get.

So
a.b@1234567890123.com is invalid whilst
a.b@123456789012.com and a.b@1234567890123.uk are both valid?

Can anyone help please?
Regards.
Patrick.
TOPICS
Server side applications

Views

252
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 28, 2008 Jul 28, 2008

Copy link to clipboard

Copied

.oO(patricktr)

> I've picked up a simple e-mail vaildation string from one of the forums which
>seems to work (most times)...

Proper regex-based email validation is almost impossible. For an example
see this little pattern, which is still incomplete:

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

And even if the address appears to be syntactially correct doesn't mean
that it exists. The only way to be sure is to actually send a message
and have the user respond.

> I've just hit a problem where it is rejecting an e-mail address and I can't
>see why.
>
> The vailidation statment is....
>
> if
>(preg_match("/^([a-z0-9]([a-z0-9_-]*\.?[a-z0-9])*)(\+[a-z0-9]+)?@([a-z0-9]([a-z0
>-9-]*[a-z0-9])*\.)*([a-z0-9]([a-z0-9-]*[a-z0-9]+)*)\.[a-z]{2,6}$/", $address))
> { return true; }
> else
> { return false; }

Such patterns will always reject a lot of valid addresses and allow many
invalid ones (hint: there are a _lot_ more chars allowed than just ASCII
letters and digits!). And this pattern doesn't even allow uppercase ...

If it has to be a regex check, I would simply check for a single '@' and
some string before and after it.

Micha

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2008 Jul 28, 2008

Copy link to clipboard

Copied

LATEST
Thanks Micha. I appreciate what you are saying and I knew that the statement was far from all-encompassing but I can't see why it is limiting the bit beyond the '@' sign to 16 chars? Does any know or has anyone got a better, simple e-mail address validator.

P.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines