Jump to content

PHP Valid Email


Recommended Posts

So i would use this? 

 

<?php

$email = "[email protected]";

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
  echo "Valid email address.";
}
else {
  echo "Invalid email address.";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797785
Share on other sites

I you want to make sure the email address actually even looks like an email address, Try this tutorial, its came in very handy to me when writing regex. Check the Matching Patterns section for the email validation code.

 

If you want to validate their email address, once they enter the address you'd create a random string

(something like md5(rand()."salt string")) and save the string, and there email address in a table with 2 other fields, a unique id and a field determining whether or not the email has been validated. Use the random string in a url like www.domain.com/validate.php?key=flhdi7fgi3hkjnfp92u2hl9f3 and email that to them... they click on the link and when they get there the table is updated to say that that email address has been validated.

Link to comment
https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797787
Share on other sites

its entirely possible there is something wrong with the regex. I didn't write it, i just did a google search and picked a result.

 

You can do your own search, pick a regex function to try and see if it works.  There's tons of them out there

 

edit: i tested it and the regex works just fine

 

"[email protected]" validates

"someoneexample.com" does not validate

Link to comment
https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797803
Share on other sites

Untested, but I would suggest against eregi as it is being depreciated.

 

<?php
$email = "[email protected]";

$pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' .
'(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';

$valid = preg_match($pattern, $email);

if ($valid == 1) {
    echo 'Valid email';
}else {
    echo 'InValid email';
}

?>

 

Pulled from preg_match user comments.

 

That will only solve the issue of a valid email address. To make sure the domain has an mx record you can use checkdnsrr to validate that portion (the host part):

 

<?php
function validateEmail($email) {
   list(,$host) = explode("@", $email);

   if (empty($host)) 
      return false;

   if (!checkdnsrr($host, "MX")) 
      return false;
    $pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' .
     '(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';

   return preg_match($pattern, $email);
}

if (validateEmail('[email protected]')) {
    echo 'Email valid.';
}else {
    echo 'Email not valid.';
}
?>

 

The code above is untested, so may have issues with syntax, fix those and it should work.

Link to comment
https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797818
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.