0

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.";
}
?>
Community
  • 1
  • 1
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • 2
    You can use `` for that. Or use PHPs built-in regex for verifying an email with `filter_var($email, FILTER_VALIDATE_EMAIL)`. Yours is maybe too basic. (Both examples that you've been alternating here.) – mario Sep 01 '11 at 17:16
  • Your regex wouldn't for example match emails like: mailtest@пример.испытание or mailtest@例え.テスト see: http://idn.icann.org/E-mail_test – feeela Sep 01 '11 at 17:27
  • @Nikolai: The Question was downvoted because you didn't search for an answer that is already there. This site isn't about duplicating the same questions and answers over and over again. – fresskoma Sep 02 '11 at 08:53
  • @Kaoukkos - Guess what? You have no control over what we do. Get over it. –  Jun 15 '12 at 22:11

4 Answers4

25

Try:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo 'Valid';
} else {
  echo 'Invalid';
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
7

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.";
}
Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469
  • +1. This is absolutely what you should be doing, if you're going to try validating emails. Validating an email is not an easy thing to do, and filter_var does as good of job as anything. That said, email validation is not strictly necessary, as it will tell you nothing about whether or not the email is actually real/working. – mfonda Sep 01 '11 at 17:23
2

Apart from a regex, you can also use filter_var:

if (filter_var('someone@example.com', FILTER_VALIDATE_EMAIL)) === false)
{
  echo "Invalid email address.";
}
jeroen
  • 91,079
  • 21
  • 114
  • 132
0

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:

  • "Abc@def"@example.com
  • "Fred Bloggs"@example.com
  • "Joe\Blow"@example.com
  • "Abc@def"@example.com
  • customer/department=shipping@example.com
  • $A12345@example.com
  • !def!xyz%abc@example.com
  • _somename@example.com

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.

Community
  • 1
  • 1
fresskoma
  • 25,481
  • 10
  • 85
  • 128