need help with email validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
darkfreaks
Forum Commoner
Posts: 59
Joined: Sat Sep 09, 2006 3:59 pm

need help with email validation

Post by darkfreaks »

ok so i tried a validation code and it doesnt work could someone tell me why? it doesnt check to see if it is false ot not it just sends the mail



here is my code:

Code: Select all

<?php 

function checkEmail($email) 
{
   if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
   {
      return FALSE;
   } 

if(checkEmail($email) == FALSE)
{ $emailerror = "Email must be filled and valid.<br />"; }

}
?>



<? echo $emailerror; ?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I take it you didn't copy and paste properly, but either way, your function can be reduced to

Code: Select all

function checkEmail($email)
{
   return eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email));
}
That should correct your problem. You were basically saying, if the pattern matched a valid email, return a failure -- which is the opposite of which you wanted.
darkfreaks
Forum Commoner
Posts: 59
Joined: Sat Sep 09, 2006 3:59 pm

Post by darkfreaks »

im looking for something like i had above that checks the email and if it returns false then echos email error next to the field of the posted email. if its a valid email i want it to send but if not then to error
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

darkfreaks wrote:im looking for something like i had above that checks the email and if it returns false then echos email error next to the field of the posted email. if its a valid email i want it to send but if not then to error
I know that.. why don't you try it.
darkfreaks
Forum Commoner
Posts: 59
Joined: Sat Sep 09, 2006 3:59 pm

Post by darkfreaks »

i did it just sends the email everything ive tried doesnt work thats what im saying! it just sends the email it doesnt actually error or anything
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

darkfreaks wrote:i did it just sends the email everything ive tried doesnt work thats what im saying! it just sends the email it doesnt actually error or anything
post your full code then
darkfreaks
Forum Commoner
Posts: 59
Joined: Sat Sep 09, 2006 3:59 pm

Post by darkfreaks »

Code: Select all

<?php
} else {





//define variable and post



$NNSQTY = $_POST[NNSQTY];
$CBFNQTY = $_POST[CBFNQTY];
$AKNQTY = $_POST[AKNQTY];
$CFNQTY = $_POST[CFNQTY];
$CVNQTY = $_POST[CVNQTY];
$name = $_POST[name];
$NNSBT = $_POST[NNSBT];
$CBFNBT = $_POST[CBFNBT];
$AKNBT = $_POST[AKNBT];
$CFNBT = $_POST[CFNBT];
$CVNBT = $_POST[CVNBT];
$NNS_size = $_POST[NNS_size];
$CBFN_size = $_POST[CBFN_size];
$AKN_size = $_POST[AKN_size];
$CFN_size = $_POST[CFN_size];
$CVN_size = $_POST[CVN_size];
$NNSBS = $_POST[NNSBS];
$CBFNBS = $_POST[CBFNBS];
$AKNBS = $_POST[AKNBS];
$CFNBS = $_POST[CFNBS];
$CVNBS = $_POST[CVNBS];
$NNSBC = $_POST[NNSBC];
$CBFNBC = $_POST[CBFNBC];
$AKNBC = $_POST[AKNBC];
$CFNBC = $_POST[CFNBC];
$CVNBC = $_POST[CVNBC];
$NNSP = $_POST[NNSP];
$CBFNP = $_POST[CBFNP];
$AKNP = $_POST[AKNP];
$CFNP = $_POST[CFNP];
$CVNP = $_POST[CVNP];
$NNSL = $_POST[NNSL];
$CBFNL = $_POST[CBFNL];
$AKNL = $_POST[AKNL];
$CFNL = $_POST[CFNL];
$CVNL = $_POST[CVNL];
$email = $_POST[email];


//bead type fees

$NNSBT=$_POST[NNSBT] ; if($NNSBT=="Glass") {$NNSFEE=0;} elseif($NNSBT=="Stone") {$NNSFEE=10;}
$CBFNBT=$_POST[CBFNBT] ; if($CBFNBT=="Glass") {$CBFNFEE=0;} elseif($CBFNBT=="Stone") {$CBFNFEE=10;};
$AKNBT=$_POST[AKNBT] ; if($AKNBT=="Glass") {$AKNFEE=0;} elseif($AKNBT=="Stone") {$AKNFEE=10;};
$CFNBT=$_POST[CFNBT] ; if($CFNBT=="Glass") {$CFNFEE=0;} elseif($CFNBT=="Stone") {$CFNFEE=10;}
$CVNBT=$_POST[CVNBT] ; if($CVNBT=="Glass") {$CVNFEE=0;} elseif($CVNBT=="Stone") {$CVNFEE=10;}

/// bead size fees
$NNSBS=$_POST[NNSBS] ; if($NNSBS=="6mm") {$NNSPAY=0;} elseif($NNSBS=="8mm") {$NNSPAY=5;}
$CBFNBS=$_POST[CBFNBS] ; if($CBFNBS=="6mm") {$CBFNPAY=0;} elseif($CBFNBS=="8mm") {$CBFNPAY=5;}
$AKNBS=$_POST[AKNBS] ; if($AKNBS=="6mm") {$AKNPAY=0;} elseif($AKNBS=="8mm") {$AKNPAY=5;}
$CFNBS=$_POST[CFNBS] ; if($CFNBS=="6mm") {$CFNPAY=0;} elseif($CFNBS=="8mm") {$CFNPAY=5;}
$CVNBS=$_POST[CVNBS] ; if($CVNBS=="6mm") {$CVNPAY=0;} elseif($CVNBS=="8mm") {$CVNPAY=5;}

////bead chain size
$NNS_size=$_POST[NNS_size] ; if($NNS_size=="Small") {$NNSMONEY=0;} elseif($NNS_size=="Large") {$NNSMONEY=5;}
$CBFN_size=$_POST[CBFN_size] ; if($CBFN_size=="Small") {$CBFNMONEY=0;} elseif($CBFN_size=="Large") {$CBFNMONEY=5;}
$AKN_size=$_POST[AKN_size] ; if($AKN_size=="Small") {$AKNMONEY=0;} elseif($AKN_size=="Large") {$AKNMONEY=5;}
$CFN_size=$_POST[CFN_size] ; if($CFN_size=="Small") {$CFNMONEY=0;} elseif($CFN_size=="Large") {$CFNMONEY=5;}
$CVN_size=$_POST[CVN_size] ; if($CVN_size=="Small") {$CVNMONEY=0;} elseif($CVN_size=="Large") {$CVNMONEY=5;}

//bead coloe fee
$NNSBC=$_POST[NNSBC] ; if($NNSBC=="Red") {$NNSCASH=0;} elseif($NNSBC=="Black") {$NNSCASH=0;}elseif($NNSBC=="Blue") {$NNSCASH=0;}
elseif($NNSBC=="Green") {$NNSCASH=0;}elseif($NNSBC=="Black") {$NNSCASH=0;}elseif($NNSBC=="Rose Quartz") {$NNSCASH=10;}


//define price variables and functions

$NNSPRICE = $NNSQTY * (30+$NNSFEE+$NNSPAY+$NNSMONEY);
$CBFNPRICE = $CBFNQTY * (30+$CBFNFEE+$CBFNPAY+$CBFNMONEY);
$AKNPRICE = $AKNQTY * (50+$AKNFEE+$AKNPAY+$AKNMONEY);
$CFNPRICE = $CFNQTY *(50+$CFNFEE+$CFNPAY+$CFNMONEY);
$CVNPRICE = $CVNQTY* (50+$CVNFEE+$CVNPAY+$CVNMONEY);




///total price
$totalamount = $NNSPRICE+$CBFNPRICE+$AKNPRICE+$CFNPRICE+$CVNPRICE;


// total quantity
$totalqty= $NNSQTY+$CBFNQTY+$AKNQTY+$CFNQTY+$CVNQTY;

//mail
$to = "mowtripleh@hotmail.com";
$subject = "Order from " . $name;
$headers = "From: $email\r\nContent-type:text/html\r\n";
$message = "<b><font face=\"arial\" size=\"3\" color=\"pink\">" . $name . " has ordered the following items:</font>\n
\n
<table>\n
<tr><td>Necklace:</td><td>Length</td><td>Bead Size</td><td>Bead Type</td><td>Quantity</td><td>Subtotal:</td></tr>\n
<tr><td>Necklace of the Night Sky:</td><td> " . $NNSL . " </td><td> " . $NNSBS . " </td><td> " . $NNSBT . " </td><td> " . $NNSQTY . " </td><td>\$" . $NNSPRICE . "</td></tr>\n
<tr><td>Chainmaile Beaded FancyNecklace:</td><td> " . $CBFNL . " </td><td> " . $CBFNBS . " </td><td> " . $CBFNBT . " </td><td>" . $CBFNQTY . "</td><td>\$" . $CBFNPRICE . "</td></tr>\n
<tr><td>Antique Key Necklace:</td><td> " . $AKNL . " </td><td> " . $AKNBS . " </td><td> " . $AKNBT . " </td><td>" . $AKNQTY . "</td><td>\$" . $AKNPRICE . "</td></tr>\n
<tr><td>Chainmaile Fancy Necklace:</td><td> " . $CFNL . " </td><td> " . $CFNBS . " </td><td> " . $CFNBT . " </td><td>" . $CFNQTY . "</td><td>\$" . $CFNPRICE . "</td></tr>\n
<tr><td>Chainmaile V Necklace:</td><td> " . $CVNL . " </td><td> " . $CVNBS . " </td><td> " . $CVNBT . " </td><td>" . $CVNQTY . "</td><td>\$" . $CVNPRICE . "</td></tr>\n
<tr><td colspan=\"6\"><hr></td></tr>
<tr><td>Total:</td><td></td><td></td><td></td><td></td><td>\$" . $totalamount . "</td></tr>\n
</table>";
if (mail($to,$subject,$message,$headers)) {echo 'Your order has been sent to ', $to, ' from ', $email, ' with the subject ', $subject, ' and the following message:<br>', $message, '.';} else {echo "Something went wrong. Please send an email directly to admin@tatoos-n-piercings.com";}
}
?>
z
<?php 

function checkEmail($email) 
{ 
   return eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)); 
}
?>
Last edited by darkfreaks on Tue Dec 19, 2006 10:35 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I meant all relevant code.. please edit your code to reflect this.

Also, you arn't even calling your function, or even have a conditional whether or not to send.
darkfreaks
Forum Commoner
Posts: 59
Joined: Sat Sep 09, 2006 3:59 pm

Post by darkfreaks »

so the function and if statements need to be before the mail statement im pretty sure thats why it was always mailing.
darkfreaks
Forum Commoner
Posts: 59
Joined: Sat Sep 09, 2006 3:59 pm

Post by darkfreaks »

so would i put

Code: Select all

if 
function check_email_address($email) {


  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {

  return false;



(mail($to,$subject,$message,$headers)) {echo 'Your order has been sent to ', $to, ' from ', $email, ' with the subject ', $subject, ' and the following message:<br>', $message, '.';} else {echo "Something went wrong. Please send an email directly to admin@tatoos-n-piercings.com";}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

if (check_email_address($email)) 
{
   //mail
}
else
{
   //output error
}
darkfreaks
Forum Commoner
Posts: 59
Joined: Sat Sep 09, 2006 3:59 pm

Post by darkfreaks »

would this work?

Code: Select all

function check_email_address($email) {


  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {

  return false;

if ($email ==false) { $emailerror = "Email must be filled and valid.<br />"; }

if($email ==true)  {(mail($to,$subject,$message,$headers))}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

No.

Code: Select all

function check_email_address($email) 
{
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) 
   {
      return false;

      if ($email ==false) 
      { 
         $emailerror = "Email must be filled and valid.<br />"; 
      }

      if($email ==true)  
      {
         mail($to,$subject,$message,$headers)
      }
1. if you indented properly you would see this would cause a parse error.
2. Once a function hits a return, the function terminates
3. $email will never evaluate to true, because it will only get to that point if it fails the eregi()
4. $to, $subject, $message, $headers are are variables outside the function scope.

Why don't you do it the way I setup in my last post?

Just a note, never ask "will this work" because I will not reply considering you can try it and find out for yourself. My time is precious thanks. :wink:
Post Reply