PHP Regex Valid Email Check

By gilbitron in PHP on 23/02/2009 at 06:21

Views: 558
Tagged: regex, php
URL: http://www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm

Starter:

If you want a PHP script to verify an email address then use this quick and simple PHP regular expression for email validation. This is also case-insensitive, so it will treat all characters as lower case. It is a really easy way to check the syntax and format of an email address.

Main Course:

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

Expand Report Code | Install Coda Clip

Please log in to vote.

Compliments to the Chef

  1. Comment by Dave on 24/02/2009 at 10:01

    nice regex. The only change needed is to allow domains that contain 4 characters - unless you don't care about .info addresses (and who really cares about .info anyway... amiritebro?)

How was your Meal?


  • Allowed HTML: <b><strong><u><i><em><a>

  • Why ask? It helps us stop spam comments.