Using regex for simple email validation

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

尝试:

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

Your regular expression unnecessarily forbids subdomains, such as user@unit.company.com . 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.";
}

除了正则表达式,你也可以使用filter_var

if (filter_var('someone@example.com', FILTER_VALIDATE_EMAIL)) === false)
{
  echo "Invalid email address.";
}
链接地址: http://www.djcxy.com/p/16566.html

上一篇: 正则表达式有效的电子邮件地址

下一篇: 使用正则表达式进行简单电子邮件验证