Jump to content

verify email's format and if already used


harkly

Recommended Posts

I am trying to verify if the email is in the correct format and then if it is already in the database.

 

I am using Ajax to do this but think the issue is on the php end.

 

Right now everything seems to be working but the checking the database, whatever I enter comes out as though it the email is not in the database.

 

Can someone check my code and see what I am doing wrong?

 

<?php


// This is a sample code in case you wish to check the userID from a mysql db table


include('library/login.php');
login();
mysql_select_db('test'); 

$email = $_POST['email'];

$sql_check = mysql_query("select email from user where email='".$email."'") or die(mysql_error());


if(isSet($_POST['email']))
  {
    if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email))
      {
        echo "<font color=red> Invalid email</font> \n";
      }
    elseif (mysql_num_rows($sql_check))
      {
        echo "<font color='red'>The e-mail ".$email." is already in use.</font> \n";
      }
    else
      {
        echo "OK";
       }
}

?> 

save your self a query!!!!

 

check the format 1st - if its OK THEN do the query...

 

If you are using the php >= 5.2 the you could do this...

 

?php


// This is a sample code in case you wish to check the userID from a mysql db table


include('library/login.php');
login();
mysql_select_db('test'); 

$email = isset($_POST['email']) ? $_POST['email'] : NULL;

if  ( !is_null ( $email ) )
{
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)))
    {
        echo "<font color=red> Invalid email</font> \n";
    }
    else
    {
        $sql_check = mysql_query("select email from user where email='".$email."'") or die(mysql_error());
        if (mysql_num_rows($sql_check))
        {
            echo "<font color='red'>The e-mail ".$email." is already in use.</font> \n";
        }
        else
        {
            echo "OK";
        }
    }
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.