multiple email addresses validation

the mail() function

Moderators: macek, egami, gesf

pyramix
New php-forum User
New php-forum User
Posts: 1
Joined: Wed May 11, 2011 7:32 pm

multiple email addresses validation

Postby pyramix » Wed May 11, 2011 7:36 pm

I've been assigned to a task to do a function validating email addresses. I've been using explode and eregi in the coding.. I'm newbie in php and I hope you guys can help me.

below is my code..

<?php

function valid_mail($sentence){
$pattern= "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$";$sentence = $_POST['email'];
$words = explode(',', $sentence);
foreach($words as $word){
if (ereg($pattern, $sentence))
{echo "yes!";}
else {echo "no!??";} }


?>

<html>
<head>
<title>Email validation form</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="emailForm">
<table>
<tr><td>Email:<input name="email"></td><tr>
<tr><td><input type="submit" name="submitemail" value="Validate email"></td></tr>
</table>
</form>
</body>
</html>

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: multiple email addresses validation

Postby egami » Thu May 12, 2011 5:15 am

Here's an email validator that works reliably.

Code: Select all

function validEmail($email)
{
   
$isValid true;
   
$atIndex strrpos($email"@");
   if (
is_bool($atIndex) && !$atIndex)
   {
      
$isValid false;
   }
   else
   {
      
$domain substr($email$atIndex+1);
      
$local substr($email0$atIndex);
      
$localLen strlen($local);
      
$domainLen strlen($domain);
      if (
$localLen || $localLen 64)
      {
         
// local part length exceeded
         
$isValid false;
      }
      else if (
$domainLen || $domainLen 255)
      {
         
// domain part length exceeded
         
$isValid false;
      }
      else if (
$local[0] == '.' || $local[$localLen-1] == '.')
      {
         
// local part starts or ends with '.'
         
$isValid false;
      }
      else if (
preg_match('/\\.\\./'$local))
      {
         
// local part has two consecutive dots
         
$isValid false;
      }
      else if (!
preg_match('/^[A-Za-z0-9\\-\\.]+$/'$domain))
      {
         
// character not valid in domain part
         
$isValid false;
      }
      else if (
preg_match('/\\.\\./'$domain))
      {
         
// domain part has two consecutive dots
         
$isValid false;
      }
      else if (!
preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 
str_replace("\\\\","",$local)))
      {
         
// character not valid in local part unless
         // local part is quoted
         
if (!preg_match('/^"(\\\\"|[^"])+"$/',
             
str_replace("\\\\","",$local)))
         {
            
$isValid false;
         }
      }
      if (
$isValid && !(checkdnsrr($domain,"MX") || (checkdnsrr($domain,"A"))))
      {
         
// domain not found in DNS
         
$isValid false;
      }
   }
   return 
$isValid;
}
 

* I did not write this, I found this script out on the net. So the credit goes to that person. Unfortunately, I don't recall where or who it was from. *


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest

cron