#native_company# #native_desc#
#native_cta#

Email Validator

By Neil Sidlow
on May 7, 2003

Version: 2.1

Type: Sample Code (HOWTO)

Category: Algorithms

License: GNU General Public License

Description: Will check to see if an email address is in the proper format.

<?php
function emailsyntax_is_valid($email) {
  $to_work_out = explode("@", $email);
  if (!isset($to_work_out[0])) return FALSE;
  if (!isset($to_work_out[1])) return FALSE;

  $pattern_local = '^([0-9a-z]*([-|_]?[0-9a-z]+)*)(([-|_]?).([-|_]?)[0-9a-z]*([-|_]?[0-9a-z]+)+)*([-|_]?)$';
  $pattern_domain = '^([0-9a-z]+([-]?[0-9a-z]+)*)(([-]?).([-]?)[0-9a-z]*([-]?[0-9a-z]+)+)*.[a-z]{2,4}$';
  $match_local = eregi($pattern_local, $to_work_out[0]);
  $match_domain = eregi($pattern_domain, $to_work_out[1]);

  if ($match_local && $match_domain) {
    return TRUE;
  }
  return FALSE;
}
?>

Use the function like this :

if (emailsyntax_is_valid($email)) {
  echo "Ok";
} 

or

if (!emailsyntax_is_valid($email)) {
  echo "Invaild";
}