Newb
Newb

Reputation: 678

php regex too strict

Just wondering why this is too strict, I can send very simplified emails to say [email protected] or [email protected]

but if I make the email any longer ([email protected]) it does not get sent.

Instead it echos back my error message:Invalid Email Address Supplied

    // Create a function to check email 
function checkEmail($email)
{
// Add some regex 
  return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
}

Upvotes: 0

Views: 210

Answers (4)

Arkh
Arkh

Reputation: 8459

If you have access to php 5.2 or above, you should use the filter functions :

function checkEmail($email){
  return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

Or validate it, "the right way".

Upvotes: 3

ghostdog74
ghostdog74

Reputation: 342819

I usually don't fret myself too much for checking email validity. I just need to check there is a value in front of "@" and at the back. That's all. The rest of the "checking" job, the MTAs will do that for me. If its invalid email, i will get a response from MTA. If its able to be sent out, that means the email is most probably valid.

Upvotes: 1

Ed Guiness
Ed Guiness

Reputation: 35277

This part

@[\w\d.-]{2,}

is gobbling up

@gmail.com

leaving nothing for this part

[\w\d.-]{2,}

to match.

Better to reuse something already proven, see for example http://www.regular-expressions.info/email.html

Upvotes: 2

tuergeist
tuergeist

Reputation: 9401

please try

'/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/'

it also fits for subdomain email adresses as [email protected]

Upvotes: 0

Related Questions