Validate Email with PHP

November 8, 2005 by  
Filed under Daily Ramblings

Here’s a great way to validate email addresses using PHP.

Related Posts Plugin for WordPress, Blogger...
  • Roy

    Ooh, thanks a bunch! This will save me a few hours :)

  • Roy

    Ooh, thanks a bunch! This will save me a few hours :)

  • http://www.adamthedog.com Adam

    That’s really useful. I’ve been using the code below. I usually comment out the domain check below (get by host name function) because it occasionally fails on a valid domain. I don’t know why…

    if( !eregi(“^[a-z0-9]+([_\.-][a-z0-9]+)*”.”@([a-z0-9]+([.-][a-z0-9]{1,})+)*$”,$mail, $regs) )
    {
    echo(“I’m sorry, ‘$mail’ is not a valid mail address.n”);
    die();
    }

    elseif( gethostbyname($regs[2]) == $regs[2] )
    {
    echo(“I’m sorry, but ‘$regs[2]‘ is not a valid domain.n”);
    die();
    }

  • http://www.adamthedog.com Adam

    That’s really useful. I’ve been using the code below. I usually comment out the domain check below (get by host name function) because it occasionally fails on a valid domain. I don’t know why…

    if( !eregi(“^[a-z0-9]+([_\\.-][a-z0-9]+)*”.”@([a-z0-9]+([\.-][a-z0-9]{1,})+)*$”,$mail, $regs) )
    {
    echo(“I’m sorry, ‘$mail’ is not a valid mail address.\n”);
    die();
    }

    elseif( gethostbyname($regs[2]) == $regs[2] )
    {
    echo(“I’m sorry, but ‘$regs[2]‘ is not a valid domain.\n”);
    die();
    }