Validation Script for email

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

Here is my script

$domains = array('', '');

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

if($_GET["inputid"] == "112" && $_GET["value"] == "$pattern")
    echo "lz_validate_input_result(true,112);";
    echo "lz_validate_input_result(false,112);";

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

So I need it that if it’s not []or [] 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('', '');
$pattern = "/(" . implode('|', $domains) . ")$/i";

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

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:

for example…

Sponsor our Newsletter | Privacy Policy | Terms of Service