使用正则表达式进行简单的电子邮件验证

发布于 2024-12-02 09:47:16 字数 809 浏览 0 评论 0 原文

可能的重复:
验证电子邮件地址的最佳正则表达式是什么?< /a>
是否有用于电子邮件地址验证的 php 库?

在我的注册表单上,用户将添加他的电子邮件,然后收到一封电子邮件来验证他的帐户。不过,我想要进行简单的电子邮件验证,并且我想知道以下内容是否合适。

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

Possible Duplicates:
What is the best regular expression for validating email addresses?
Is there a php library for email address validation?

On my register form a user will add his email and then get an email to verify his account. However I want to have a simple email validation and I would like to know if the following is appropriate.

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

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

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

发布评论

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

评论(4

污味仙女 2024-12-09 09:47:16

尝试:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo 'Valid';
} else {
  echo 'Invalid';
}

Try:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo 'Valid';
} else {
  echo 'Invalid';
}
深海蓝天 2024-12-09 09:47:16

您的正则表达式不必要地禁止子域,例如 [email protected] 。此外,您不应使用已弃用的 eregi

不要重新发明自己的轮子,而是使用 filter_var

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo "Valid email address.";
} else {
  echo "Invalid email address.";
}

Your regular expression unnecessarily forbids subdomains, such as [email protected]. Additionally, you shouldn't use the deprecated eregi.

Instead of reinventing your own wheel, use filter_var:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo "Valid email address.";
} else {
  echo "Invalid email address.";
}
神经大条 2024-12-09 09:47:16

除了正则表达式之外,您还可以使用 filter_var

if (filter_var('[email protected]', FILTER_VALIDATE_EMAIL)) === false)
{
  echo "Invalid email address.";
}

Apart from a regex, you can also use filter_var:

if (filter_var('[email protected]', FILTER_VALIDATE_EMAIL)) === false)
{
  echo "Invalid email address.";
}
诗笺 2024-12-09 09:47:16

电子邮件验证有点棘手,因为 RFC 指定了电子邮件的格式地址比人们想象的更不受限制。例如,以下都是有效的电子邮件地址:

电子邮件验证的正确正则表达式似乎类似于 这个,我什至不会尝试研究它;)但是,在使用 PHP 时,您确实应该使用 filter_var(),如下所示:

$valid = filter_var($email, FILTER_VALIDATE_EMAIL);
# $valid will be TRUE if the email address was valid,
# otherwise FALSE.

RFC822 似乎包含原始电子邮件格式规范,但我认为 2822(如上面链接的)只是原始 RFC 的修订/修正。

Email validation is a bit tricky, because the RFC that specifies the format of email addresses is way more unrestrictive that people think. For example, the following are all valid email addresses:

The correct regex for email validation seems to look something like this, which I won't even try to look into ;) However, when using PHP, you should really use filter_var(), like so:

$valid = filter_var($email, FILTER_VALIDATE_EMAIL);
# $valid will be TRUE if the email address was valid,
# otherwise FALSE.

RFC822 seems to contain the original email format specification, but I think that 2822 (as linked above) is just a revision / amendment of that original RFC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文