Validation Script for email

I am trying to get the email validation working via php but having issues.

Here is my script

<?php
$domains = array('@domain1.com', '@domain2.com');

    $pattern = "/^[a-z0-9._%+-](" . implode('|', $domains) . ")$/i";

if($_GET["inputid"] == "112" && $_GET["value"] == "$pattern")
{
    echo "lz_validate_input_result(true,112);";
    exit();
}
else
{
    echo "lz_validate_input_result(false,112);";
    exit();
}
?>

hits the script with email.php?inputid=112&[email protected]

So I need it that if it’s not [domain1.com]or [domain2.com] it returns a false.

Can someone help me rewrite this for it works. I am a php newbie.

Well, to do this and be secure, you need to grab the “get” variable, filter it to protect from hackers and
then handle the domain section. Something like this should work. (Not tested!)

//  Get the email address (variable "value")
if (!filter_input(INPUT_GET, "value", FILTER_VALIDATE_EMAIL)) {
    //  Email value is NOT correctly formated
    echo("Email is not valid");
} else {
    //  Email is formed correctly, now test it for the correct domain name
    $temp = explode("@", filter_input(INPUT_GET, "value"));
    if (in_array($temp[1], $domains) {
        //  Input domain is in list, handle as needed...
    } else {
        //   Input domain is NOT in list, throw out error
    }
}

Heres is another approach: (example only)

$domains = array('@domain1.com', '@domain2.com');
$pattern = "/(" . implode('|', $domains) . ")$/i";

//$testEmail = $_GET["value"];
$testEmail = '[email protected]'

if (preg_match($pattern, $testEmail)) {
 echo "true";
} else { 
 echo "false";
}

This worked, thank you.

Might want to add back in those @ signs in the domain ARRAY…

so that you dont get false positives on emails like:

[email protected]

for example…

Sponsor our Newsletter | Privacy Policy | Terms of Service