function eregi() is deprecated in email validation

4.4k Views Asked by At

Hi ı know that we do not eregi but preg_match but when ı change only eregi code it doesnt work, how can ı change the code below please just a little help, ı am a newbie

function verify_valid_email($emailtocheck)
{
    $eregicheck = "^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,4}\$";
    return eregi($eregicheck, $emailtocheck);
}

function verify_email_unique($emailtocheck)
{
    global $config,$conn;
    $query = "select count(*) as total from members where email='".mysql_real_escape_string($emailtocheck)."' limit 1"; 
    $executequery = $conn->execute($query);
    $totalemails = $executequery->fields[total];
    if ($totalemails >= 1)
    {
        return false;
    }
    else
    {
        return true;
    }
}
2

There are 2 best solutions below

2
On

If you need to validate e-mail addresses, you can look at this page which provides a working example using only filter_var() :

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_a) email address is considered valid.";
};

So in your code, you should just drop all the regex/eregi stuff and use this instead :

return filter_var($emailtocheck, FILTER_VALIDATE_EMAIL);
0
On

If you want to do it this way, you can base yourself on the following methods:

<?php 
$email = \"abc123@somewhere\"; // Invalid email address 
//$email = \"[email protected]\"; // Valid email address 
// Set up regular expression strings to evaluate the value of email variable against
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
// Run the preg_match() function on regex against the email address
if (preg_match($regex, $email)) {
     echo $email . \" is a valid email. We can accept it.\";
} else { 
     echo $email . \" is an invalid email. Please try again.\";
} 
?>

or:

$string = "$emailtocheck";
if (preg_match(
'/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/',
$string)) {
echo "Successful.";
}

or:

<?php
$email = "[email protected]"; 
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
if (preg_match($regex, $email)) {
 echo $email . " is a valid email. We can accept it.";
} else { 
 echo $email . " is an invalid email. Please try again.";
}           
?>

Source: https://stackoverflow.com/a/13719991/1415724

or:

<?php
// check e-mail address
// display success or failure message
if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-
])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $_POST['e-mail'])) {
    die("Invalid e-mail address");
}
echo "Valid e-mail address, processing...";
?>

Source: http://www.techrepublic.com/article/regular-expression-engine-simplifies-e-mail-validation-in-php/


Plus, you can try what André Daniel wrote as an answer as well. You have many choices.