Jump to content

email verification


ahazin

Recommended Posts

I have created a form which allows the user to input name, email address and a message this is then sent to a php page which sends the information on to my email address. This is all working fine, but when i tried to add email validation it didnt seem to work for me.  I simply wanted to ensure the user had input the @ symbol, nothing more difficult than that.  Though if it was easy to implement more/better validation that would be a bonus. 

 

this is my code so far: 

 

*Note* i have 2 txt boxs called name_field and email_field  and a text area called message.

<?php 
$to = "lucealmighty@hotmail.co.uk";
$subject = "Contact Us";
$name_field = $_REQUEST['name_field'] ;
$email_field = $_REQUEST['email_field'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email_field";
$body = "Name: $name_field \n E-Mail: $email_field \n Message:\n $message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">Your message was sent successfully</strong></font>"); }
else
{print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">We encountered an error sending your message.  Please try again or call us on 07802 414985</strong></font>"); }
?>

 

When inputting the validation in i input this code:

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

if(checkEmail(email_field) == FALSE) 
{
   echo "E-mail entered is not valid.";
} 
else 
{
   echo "E-mail entered is valid.";
}

 

When this code is input it always says the email address is not valid wether it contains an @ symbol or not.

 

Does anyone know where i could be going wrong. 

 

Would really appreciate help on this.

 

Another thing i tried to get working was this, though im not so fussed on getting this section to work though it would be a bonus:

if ($name=="" or $message=="" or $email=="") {
echo "Please fill up all fields !";

 

The above also didnt work.

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

so my full code is:

 

<?php 
function checkEmail($email_field) 
{
   if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email_field)) 
   {
      return FALSE;
   }
  else 
      {
         return TRUE; 
      }
}

$to = "lucealmighty@hotmail.co.uk";
$subject = "Contact Us";
$name_field = $_REQUEST['name_field'] ;
$email_field = $_REQUEST['email_field'] ;
$message = $_REQUEST['message'] ;
if(checkEmail(email_field) == FALSE) 
{
   echo "E-mail entered is not valid.";
} 
else 
{
   echo "E-mail entered is valid.";
}

$headers = "From: $email_field";
$body = "Name: $name_field \n E-Mail: $email_field \n Message:\n $message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">Your message was sent successfully</strong></font>"); }
else
{print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">We encountered an error sending your message.  Please try again or call us on 07802 414985</strong></font>"); }
?>

 

and for some reason no matter what i input as an email address it always says it is valid.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.