验证电子邮件地址的函数

发布于 2024-09-26 13:06:46 字数 2156 浏览 2 评论 0原文

我正在使用此功能来验证电子邮件地址, 但如果电子邮件地址是这样的就不行了:

[email protected]. 
OR 
//[email protected]

有办法开发这个功能吗?

function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $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 am using this function for validating email addresses,
but it doesn’t work if the email address is like this:

[email protected]. 
OR 
//[email protected]

Is there a way to develop this function?

function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空气里的味道 2024-10-03 13:06:46

使用filter_var()。下面是一个简单的使用演示。还有许多其他选项可供选择。

<?php

  // You might want to trim whitespace first: 
$possibleEmailAddress = trim($possibleEmailAddress);

filter_var($possibleEmailAddress, FILTER_VALIDATE_EMAIL);

// Returns false if $possibleEmailAddress doesn't appear valid.
// Returns the email string if it does appear okay.
?>

实例

请注意 //[email protected] 是有效电子邮件,但 [电子邮件受保护] 不是。您必须修剪末尾的句点才能使其有效。您不能只使用 trim(),因为电子邮件开头的句点可能是有效且有意的。

Use filter_var(). Below is a simple use demonstration. Many other options are available.

<?php

  // You might want to trim whitespace first: 
$possibleEmailAddress = trim($possibleEmailAddress);

filter_var($possibleEmailAddress, FILTER_VALIDATE_EMAIL);

// Returns false if $possibleEmailAddress doesn't appear valid.
// Returns the email string if it does appear okay.
?>

live example

Note that //[email protected] is a valid email, but [email protected]. is not. You'd have to trim the period from the end to make it valid. You can't just use trim(), since periods at the beginning of an email could be valid and intentional.

﹎☆浅夏丿初晴 2024-10-03 13:06:46
function  checkEmail($email) 
    {
    $patern = '/^[a-zA-Z0-9.\-_]+@[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,4}$/';
    if (preg_match($patern , $email)) 
        {
        return TRUE;
        }
    else
        { 
        return FALSE;
        }
    }
}

最简单的

function  checkEmail($email) 
    {
    $patern = '/^[a-zA-Z0-9.\-_]+@[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,4}$/';
    if (preg_match($patern , $email)) 
        {
        return TRUE;
        }
    else
        { 
        return FALSE;
        }
    }
}

the simplest

蓝天白云 2024-10-03 13:06:46

或者,与 Asar 相同但更短:

function  checkEmail($email) {
     $patern = '/^[a-zA-Z0-9.\-_]+@[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,4}$/';
     return preg_match($patern , $email);
}

Or, same as Asar's but shorter:

function  checkEmail($email) {
     $patern = '/^[a-zA-Z0-9.\-_]+@[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,4}$/';
     return preg_match($patern , $email);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文