Jump to content

Recommended Posts

So i would use this? 

 

<?php

$email = "someone@example.com";

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

So i would use this? 

 

<?php

$email = "someone@example.com";

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.";
}

?>

 

Looks like it would work

Link to comment
https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797789
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

 

"someone@example.com" 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 = "someone@test.com";

$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('example@sometest.com')) {
    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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.