php 一般和特定(大学域)的最佳电子邮件验证功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6232846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:43:06  来源:igfitidea点击:

Best email validation function in general and specific (college domain)?

phpemail-validation

提问by Ben

Hey guys, i know email validation is one of those things which is not the funniest thing on the block. I'm starting up a website and i want to limit my audience to only the people in my college and i also want a preferred email address for my user. So this is a two part question.

嘿伙计们,我知道电子邮件验证是其中最有趣的事情之一。我正在创建一个网站,我想将我的受众限制在我大学里的人,我还希望我的用户有一个首选的电子邮件地址。所以这是一个两部分的问题。

  1. Is there a really solid php function out there for email validation?

  2. Can i validate an email from a specific domain. I dont want to just check if the domain exists because i know www.mycollege.edu exists already. Is there really anyway to validate that the user has a valid @mycollege.edu web address? Thanks you!

  1. 是否有用于电子邮件验证的真正可靠的 php 函数?

  2. 我可以验证来自特定域的电子邮件吗?我不想只检查域是否存在,因为我知道 www.mycollege.edu 已经存在。真的有办法验证用户是否拥有有效的@mycollege.edu 网址吗?谢谢!

回答by Chris Baker

This is what I use:

这是我使用的:

   function check_email_address($email) {
        // First, we check that there's one @ symbol, and that the lengths are right
        if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
            // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
            return false;
        }
        // Split it into sections to make life easier
        $email_array = explode("@", $email);
        $local_array = explode(".", $email_array[0]);
        for ($i = 0; $i < sizeof($local_array); $i++) {
            if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\|\")]{0,62}\"))$/", $local_array[$i])) {
                return false;
            }
        }
        if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
            $domain_array = explode(".", $email_array[1]);
            if (sizeof($domain_array) < 2) {
                return false; // Not enough parts to domain
            }
            for ($i = 0; $i < sizeof($domain_array); $i++) {
                if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
                    return false;
                }
            }
        }

        return true;
    }

EDITReplaced depreciated ereg with preg_match for PHP 5.3 compliance

编辑用 preg_match 替换折旧的 ereg 以符合 PHP 5.3

回答by diggersworld

If you really want to make sure its valid make your signup form send them an email with a URL link in that they have to click to validate.

如果您真的想确保其有效,请让您的注册表单向他们发送一封带有 URL 链接的电子邮件,因为他们必须单击以进行验证。

This way not only do you know the address is valid (because the received the email), but you also know the owner of the account has signed up (unless someone else knows his login details).

通过这种方式,您不仅知道地址有效(因为收到了电子邮件),而且您还知道帐户的所有者已注册(除非其他人知道他的登录详细信息)。

To make sure it ends correctly you could use explode() on the '@' and check the second part.

为了确保它正确结束,您可以在“@”上使用 expand() 并检查第二部分。

$arr = explode('@', $email_address);
if ($arr[1] == 'mycollege.edu')
{
    // Then it's from your college
}

PHP also has it's own way of validating email addresses using filter_var: http://www.w3schools.com/php/filter_validate_email.asp

PHP 也有自己的使用 filter_var 验证电子邮件地址的方法:http: //www.w3schools.com/php/filter_validate_email.asp

回答by azat

Read here http://ru2.php.net/manual/en/book.filter.php

在这里阅读 http://ru2.php.net/manual/en/book.filter.php

Or in short

或者简而言之

var_dump(filter_var('[email protected]', FILTER_VALIDATE_EMAIL));

回答by azat

for any e-mail

对于任何电子邮件

([a-zA-Z0-9_-]+)(\@)([a-zA-Z0-9_-]+)(\.)([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?

([a-zA-Z0-9_-]+)(\@)([a-zA-Z0-9_-]+)(\.)([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?

for php preg_match function

用于 php preg_match 函数

/([a-zA-Z0-9_-]+)(\@)([a-zA-Z0-9_-]+)(\.)([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?/i

/([a-zA-Z0-9_-]+)(\@)([a-zA-Z0-9_-]+)(\.)([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?/i

for @mycollege.edu

对于@mycollege.edu

^([a-zA-Z0-9_-]+)(@mycollege.edu)$

^([a-zA-Z0-9_-]+)(@mycollege.edu)$

for php preg_match function

用于 php preg_match 函数

/^([a-zA-Z0-9_-]+)(@mycollege.edu)$/i

/^([a-zA-Z0-9_-]+)(@mycollege.edu)$/i

PHP CODE

PHP代码

<?php 

$email = '[email protected]';
preg_match('/^([a-zA-Z0-9_-]+)(@mycollege.edu)$/i', $email, $matches);

if ($matches) { 
    echo "Matched";
} else {
    echo "Not Matched";
}

var_dump($matches);

回答by azat

this might be a better solution. many answered already, eventhough its little different.

这可能是一个更好的解决方案。许多人已经回答了,尽管它有点不同。

$email = "[email protected]";

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
 echo $email ." is a valid email address";
} else {
  echo $email ." is not a valid email address";
}

I hope this one has simple to use.

我希望这个简单易用。

回答by thelightings

A simple function using filter_var in php

在php中使用filter_var的简单函数

<?php
function email_validation($email) {
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
       echo("$email is a valid email address");
    } else {
       echo("$email is not a valid email address");
    }
}

//Test
email_validation('johnson123');
?>

回答by gpresland

This should work:

这应该有效:

if (preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])@mycollege.edu$/', $email)) {
     // Valid
}