We help IT Professionals succeed at work.

URGENT! RegExp for email validation

gam3r_3xtr3m3
on
3,374 Views
Last Modified: 2012-05-04
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
Comment
Watch Question

Commented:
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";
}

Author

Commented:
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

Commented:
try this.
preg_match("/^[a-z0-9'\.\-_\+]+@[a-z0-9\-_]+\.([a-z0-9\-_]+\.)*?[a-z]+$/is", $email)
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
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

Commented:
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.

Author

Commented:
thanks all! =p

for lozloz and inxil refer to https://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20878068.html

gam3r
CERTIFIED EXPERT

Commented:
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".

Commented:
Thg,

Do you have a good list of addresses to check with?
CERTIFIED EXPERT

Commented:
I think so, why?

Commented:
I was just wondering if you could post it.  I'd like to use it to test some different RegExps...
CERTIFIED EXPERT

Commented:
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.

Commented:
i've used this one with great success:

/^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/

Commented:
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

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.