views:

190

answers:

7

Here is my code; I got the reg ex from a PHP book.

function is_email($email) {
// Checks for proper email format
if (! preg_match( '/^[A-Za-z0-9!#$%&\'*+-/=?^_`{|}~]+@[A-Za-z0-9-]+(\.[AZa-z0-9-]+)+[A-Za-z]$/', $email)) {
    return false;
} else {
    return true;
}
}

Now to test to see if the email is valid I am using:

if (is_email($_POST['email'])==TRUE){
 echo 'valid email';
}

if (is_email($_POST['email'])==FALSE){
 echo 'not valid email';
}

I must be missing something really basic. No matter what kinda email input it comes back as invalid.

Please someone show me the light.

+4  A: 

Why are you returning false and true from the functon, then testing against FALSE and TRUE in the calling code?

Ned Batchelder
I was thinking the same thing.
Homework
im noob how should I be testing?
chris
@Ned is referring to your capitalization.
nikc
oh I didnt know it made a difference. Noted. thx
chris
+9  A: 

you have an un-escaped backslash

'/^[A-Za-z0-9!#$%&\'*+-/=?^_`{|}~]+@[A-Za-z0-9-]+(\.[AZa-z0-9-]+)+[A-Za-z]$/'

should be

'/^[A-Za-z0-9!#$%&\'*+-\/=?^_`{|}~]+@[A-Za-z0-9-]+(\.[AZa-z0-9-]+)+[A-Za-z]$/'

Also, when testing an email .. use this:

if(is_email($_POST['email'])) {

        echo 'valid email';
}
else {

        echo 'not valid email';
}

One more thing, When you use dash - inside brackets, it should come either at first or last so that it is not interpreted as a range. So, your regular expression should be:

'/^[A-Za-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[A-Za-z0-9-]+(\.[AZa-z0-9-]+)+[A-Za-z]$/'

Edit There's one more mistake in the regex ... one bracket starts with AZ (missing dash). It should be

'/^[A-Za-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+[A-Za-z]$/'
Aziz
You mean unescaped slash.
Ned Batchelder
"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."
JasCav
@Ned, thanks, fixed :)
Aziz
that fixed it. ThanksI cant believe the book I bought published code with errors in it. What a ripoff. Thank god for you guys.
chris
@chris - get used to it; lots of books have insufficient editorial supervision, and even the best have occasional errata. You have to think for yourself, sometimes.
Jonathan Leffler
+1  A: 

you also could try the '===' operator to make sure that the comparision is made with booleans...

KB22
+2  A: 

In the first character class in your regular expression, you have an unescaped dash. The part of the regex that reads +-/ should probably be +\-/ because otherwise +-/ is read as a range of characters between + and /. Additonally, as others have pointed out, you have an unescaped /.

Asaph
+3  A: 

I think it is a bad idea to make an email validation regex be so complicated, you're inevitably going to not think of some format, and some user is going to be annoyed and never come back to your site. The whole point is to help the user not mistype an email, so just something like this will do it...

/^[^\s,]+\@[\w.-]+\.[a-zA-Z]{2,6}$/
jayrdub
See my comments to the question - the regex is nowhere near complex enough to handle the diversity of real email addresses.
Jonathan Leffler
A: 

Note that the only sure way to validate an email address is to send an email to it (with a unique confirmation link).

Other than that, just use a simple regex like jayrdub suggests.

therefromhere
+2  A: 

How about this: forget about super-complicated regular expressions and use PHP's own filter extension. Let PHP's developers worry about bugs and exploits and use your time building what will really make your site or application great.

Josh Davis