views:

61

answers:

2

Hey there!

I'm having a bit of a problem trying to validate email adresses using preg_match (or eregi() if that suits better). I've tried several regex patterns now and no matter what i do it doesn't seem to work.

Here's the function:

function validateEmail($email) {

    if(eregi('[a-z||0-9]@[a-z||0-9].[a-z]', $email)){
        return true;
    }

}

Any ideas what's wrong? I've tried putting an exclamation point before the eregi (and preg_match that i used before), and that reversed it all (as expected) but still didn't make it work as it should. I want it to return TRUE if it does not pass the regex.

And i didn't use the same regex when on the preg_match function, i found another one then, cause i know you can't really mix those two. Right?

Thanks in advance!

+7  A: 

You ought to use the filter extension through filter_var:

filter_var($email, FILTER_VALIDATE_EMAIL);

If you want a regex, don't use a strict rule, or my [email protected] domain will be rejected. Use something like ~[^@]+@(?:[^.]+\.)+[A-Za-z]{2,6}~. Though this will still not allow valid emails like "\@"@example.org.

PS: If you want to know why your regex doesn't work:

  • eregi is deprecated, use preg_match instead
  • inside [] don't write ||. Simply use [a-z0-9]. || would simply additionally allow the | character
  • You forgot the + quantifiers everywhere. Right know every part may have only one character.

With all those things fixed (but please don't use this regex!):

preg_match('~[a-z0-9]+@[a-z0-9]+.[a-z]+~i', $email);
nikic
I didn't even know that existed, but it sounds good. It still doesn't work for some reason though.
Nike
What exactly doesn't work?
nikic
It returns false no matter what i input. I can leave it blank and submit the form, but it still returns false. Same thing if i enter a valid email. And if i put an exclamation point before the preg_match, it inverses the action and returns true no matter what you submit.
Nike
That's it? Nothing else to contribute with?
Nike
Could you please provide us this the function code you are currently using? It should be `function validateEmail($email) { return (bool) filter_var($email, FILTER_VALIDATE_EMAIL); }`.
nikic
I finally got it working as i wanted it to. To be honest i'm not sure what was wrong before, but after playing with it for over an hour i somehow managed to get it working as it should. I used filter_var as you suggested, and it seems cool. Why are everybody suggesting regex when there already is a built-in function that validates email adresses? Oh well, thanks once again, and here's the code: function validateEmail($email) { if( !filter_var($email, FILTER_VALIDATE_EMAIL) ){ return true; } }
Nike
People simply dont know about filters, that's all ;)
nikic
Well, the filter_var function isn't around all that long, since PHP 5.2.0, when I started with PHP version 4 was just brand new...
Wrikken
+2  A: 

Just to clarify some stuff, ereg is depreciated.

To convert that to preg_match it would be:

if(preg_match('/[a-z||0-9]@[a-z||0-9].[a-z]/', $email)){

Most (I use this term loosely) ereg's just need delimiters (the first and last / ) added to be converted to preg_match.

Brad F Jacobs
Well thanks, good to know. :)
Nike