Link to home
Start Free TrialLog in
Avatar of gam3r_3xtr3m3
gam3r_3xtr3m3Flag for Philippines

asked on

URGENT! RegExp for email validation

Hello fellow experts,

i was wondering how to validate email addresses using PHP. i tries to make a RegExp for that but it didn't work.

$patternemail="/.\@.\.{2,3}?(\.{2,3})/";
if(!preg_match($patternemail,$_POST["email"])){//actions;}

Also, is there a testing for a live and active email address to avoid invalid input of email?

Thanks in advanced,
gam3r_3xtr3m3
Avatar of shivsa
shivsa
Flag of United States of America image

function validate_email($email="")
     {
          if (eregi("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,3}", $email)) return 1;
          else return 0;
     }

So you could do something like:

if (!validate_email($_REQUEST['email']))
{
     echo "<p>Sorry, but your email address is not of the valid format.</p>\n";
}
Avatar of gam3r_3xtr3m3

ASKER

thanks for the posts. but can you provide a code for the preg_match() function?
please post the code here... not the link. thanks shivsa. lolx =p
try this.
preg_match("/^[a-z0-9'\.\-_\+]+@[a-z0-9\-_]+\.([a-z0-9\-_]+\.)*?[a-z]+$/is", $email)
SOLUTION
Avatar of shivsa
shivsa
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Giovanni G
Giovanni G
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lozloz
lozloz

here's one from the manual with comments that i use

  function checkAddr($email, $field = "") {
 
     //Do the basic Reg Exp Matching for simple validation
     $regex =
       '^'.
       '[_a-z0-9-]+'.        /* One or more underscore, alphanumeric,
                                or hyphen charactures. */
       '(\.[_a-z0-9-]+)*'.  /* Followed by zero or more sets consisting
                                of a period and one or more underscore,
                                alphanumeric, or hyphen charactures. */
       '@'.                  /* Followed by an "at" characture. */
       '[a-z0-9-]+'.        /* Followed by one or more alphanumeric
                                or hyphen charactures. */
       '(\.[a-z0-9-]{2,})+'. /* Followed by one or more sets consisting
                                of a period and two or more alphanumeric
                                or hyphen charactures. */
       '$';
 
     if (eregi($regex, $email)) {
       return TRUE;
     } else {
       return FALSE;
     }
  }

loz
Here's the regexp method:

function valid ($email) {
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email))
  return false;
else
  return true;
}

You can find a whole function for connecting to the mail server, et cetera at:

http://www.zend.com/codex.php?id=449&single=1

Hope that helps.
as promised, i checked out other's suggestions.

shivsa's proposal
          if (eregi("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,3}", $email)) return 1;
won't validate "_hello_@hotmail.com", which is a perfectly legal email address. trashed.

shivsa's proposal
     preg_match("/^[a-z0-9'\.\-_\+]+@[a-z0-9\-_]+\.([a-z0-9\-_]+\.)*?[a-z]+$/is", $email)
is bad coded, so it's not worth testing at all.

again from shivsa..
    if(!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/",$email)) return TRUE;
again won't validate "_hello_@hotmail.com"

my own solution is too permissive for characters before "@", in fact i'm fixing it right now.

lozloz's one looks nice, expecially for the comments. But it won't allow emails starting with '.'. I don't know if they are legal at all..maybe it's the way it has to be.

Unfortunately email validation is a hard topic because actually I can make all of your validation regexp fail by modfying my own SMTP server for accepting mail for "!{??\n@mydomain.cxm".
Thg,

Do you have a good list of addresses to check with?
I think so, why?
I was just wondering if you could post it.  I'd like to use it to test some different RegExps...
Sorry I can't, it's not a "test list", it's rather a real emails list, and most of them are valid emails. I can't post them for obvious reasons.
i've used this one with great success:

/^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/
I've found a comprehensive mail validation function on weberdev.com.....this one actually checks the recipient server to verify that the address exists.

The address is http://www.weberdev.com/get_example.php3/536
Comment on concept9's link on weberdev. Yeap this function is good, but it uses getmxrr function, which is only available on unix so your code is less portable (ie, i code in windows, test on my own machine, and send on unix hosts).

Also i noted that using that kind of method some servers will actually reply with an error message saying that the proceeding is not allowed or something of that kind due to the fact it could as well be a spammer checking for email existance by querying the server.

So far the best way i found to check if an email is truly valid, ie, for a forum substruction or something is to :

1 ) check of the syntax is proper to begin with, some regexp examples from this very page can do the work
2 ) send an actual email which contain a link that points back to a file on your server. Once clicked that link will query a certain file which will mark the email as being valid as some1 clicked it from their inbox mail client.