-4

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;
    }
}
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118

2 Answers2

4

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);
  • 1
    +1 I'd recommend this if you can use `filter_var` over some home brewed expression thinking they got all possible emails. Although `FILTER_VALIDATE_EMAIL` isn't perfect (as far as I've heard), but its better than home brewed version. – Class Aug 27 '13 at 01:58
  • @Class I agree with you also. I figured since André already mentioned it, I did not bother repeating it in my answer. The OP has many options at his disposal. – Funk Forty Niner Aug 27 '13 at 02:05
1

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

<?php 
$email = \"abc123@somewhere\"; // Invalid email address 
//$email = \"somebody@somesite.com\"; // 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 = "abc123@sdsd.com"; 
$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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141