Dreamhost

Looking for somewhere to host your site? Browse our PHP web hosts section!

Home > Code Snippets > Validate an email address using regular expressions

Validate an email address using regular expressions

Description

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.

The code


Deprecated: Function eregi_replace() is deprecated in /home/totallyphpuk/totallyphp.co.uk/functions/function.syntaxHighlight.php on line 29

Deprecated: Function eregi_replace() is deprecated in /home/totallyphpuk/totallyphp.co.uk/functions/function.syntaxHighlight.php on line 30
<?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.";
}

?>

Get the code

Download the file to your computer: Click here to get the file