Jump to content

[SOLVED] Email Validation


GrizzlyBear

Recommended Posts

Hi

 

I have tried to find some script to validate my mailer form, but ones i have found just don't seem work. Could anyone suggest a place I can find some good script that validates text field for email and telephone numbers. I have placed the php code below.

 

Would i put the code in the PHP file called sendmail.php, or the html form page called contact.html?

 

Thanks

 

<?php

 

 

$to = 'test@domain.co.za';

$email = $_POST['email'];

$name = $_POST['name'];

$cellphone = $_POST['cellphone'];

$company = $_POST['company'];

$position = $_POST['position'];

$enquiry = $_POST['enquiry-type'];

 

 

 

 

$message = <<<MESSAGE

<html>

  <head>

    <title>Contact Form</title>

  </head>

  <body>

    <table style="font:Arial, Helvetica, sans-serif" style="font-size:12px" border="0" cellspacing="3" align="center">

      <tr>

        <td align="center" bgcolor="#999999" colspan="2"><strong>CONTACT FORM</strong></td>

  </tr>

  <tr>

        <td bgcolor="#FFFFFF" colspan="2"></td>

  </tr>

  <tr>

        <td bgcolor="#999999" colspan="2"><strong>$enquiry</strong></td>

  </tr>

  <tr>

        <td bgcolor="#999999" width="150">Name</td>

        <td bgcolor="#cccccc" width="300">$name</td>

      </tr>

  <tr>

        <td bgcolor="#999999" width="150">Cell Number</td>

        <td bgcolor="#cccccc" width="300">$cellphone</td>

      </tr>

  <tr>

        <td bgcolor="#999999" width="150">Company</td>

        <td bgcolor="#cccccc" width="300">$company</td>

      </tr>

  <tr>

        <td bgcolor="#999999" width="150">Position</td>

        <td bgcolor="#cccccc" width="300">$position</td>

      </tr>

    </table>

  </body>

</html>

MESSAGE;

 

$subject = 'CONTACT FORM';

 

$headers  = 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'From: ' . $email;

 

mail($to, $subject, $message, $headers);

header('Location: http://www.yellowwoodestates.co.za/confirmed.html');

?>

Link to comment
Share on other sites

Thanks bendude!

 

I found this script, and incorporated it in my code. I have placed the entire code below as it is not working, and perhaps someone could help me out as to what im doing wrong...

 

Thanks

 

 

 

<?php

 

if(isset($_POST['sumbit'])){

 

$to = 'info@domain.co.za';

$email = $_POST['email'];

$name = $_POST['name'];

$cellphone = $_POST['cellphone'];

$company = $_POST['company'];

$position = $_POST['position'];

$enquiry = $_POST['enquiry-type'];

 

 

 

 

$message = <<<MESSAGE

<html>

  <head>

    <title>Contact Form</title>

  </head>

  <body>

    <table style="font:Arial, Helvetica, sans-serif" style="font-size:12px" border="0" cellspacing="3" align="center">

      <tr>

        <td align="center" bgcolor="#999999" colspan="2"><strong>CONTACT FORM</strong></td>

  </tr>

  <tr>

        <td bgcolor="#FFFFFF" colspan="2"></td>

  </tr>

  <tr>

        <td bgcolor="#999999" colspan="2"><strong>$enquiry</strong></td>

  </tr>

  <tr>

        <td bgcolor="#999999" width="150">Name</td>

        <td bgcolor="#cccccc" width="300">$name</td>

      </tr>

  <tr>

        <td bgcolor="#999999" width="150">Cell Number</td>

        <td bgcolor="#cccccc" width="300">$cellphone</td>

      </tr>

  <tr>

        <td bgcolor="#999999" width="150">Company</td>

        <td bgcolor="#cccccc" width="300">$company</td>

      </tr>

  <tr>

        <td bgcolor="#999999" width="150">Position</td>

        <td bgcolor="#cccccc" width="300">$position</td>

      </tr>

    </table>

  </body>

</html>

MESSAGE;

 

$subject = 'CONTACT FORM';

 

$headers  = 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'From: ' . $email;

 

 

if(empty($name)){

echo "<b> The field name is empty.

Please fill it in </b>";

 

}else if (empty($email)){

echo "<b> The field email is empty.

Please fill it in! </b>";

 

}else if (empty($cellphone)){

echo "<b> The field Cellphone is empty.

Please fill it in! </b>";

 

 

}else{

 

mail($to, $subject, $message, $headers);

header('Location: http://www.yellowwoodestates.co.za/confirmed.html');

}

}

 

?>

Link to comment
Share on other sites

Thanks Ben

 

I managed to find an email validation script that works, but im having a problem with the number now, I have found some script, but it's not working, I think the function is right, but I think my positioning of the script is incorrect.

 

Below i have attached both php and form script...

 

 

PHP SCRIPT

 

 

<?php

 

$to = 'cheryl@a-africa.co.za';

$email = $_POST['email'];

$name = $_POST['name'];

$cellphone = $_POST['cellphone'];

$company = $_POST['company'];

$position = $_POST['position'];

$enquiry = $_POST['enquiry-type'];

 

 

function check_email_address($email) { 

// First, we check that there's one @ symbol, and that the lengths are right 

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

// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.   

return false;

}  // Split it into sections to make life easier 

$email_array = explode("@", $email); 

$local_array = explode(".", $email_array[0]); 

for ($i = 0; $i < sizeof($local_array); $i++) {

    if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {

      return false;

    }

      }   

 

if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name   

$domain_array = explode(".", $email_array[1]);   

  if (sizeof($domain_array) < 2) {

    return false; // Not enough parts to domain

 

      }   

      for ($i = 0; $i < sizeof($domain_array); $i++) {     

         if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {

            return false;

 

         }   

      } 

   } 

   

   return true;

}

 

 

function check_phone_number($cellphone) {

 

$cellphone = preg_replace('/[^0-9]/', '', $cellphone);

 

 

if (preg_match('/^1?[0-9]{10}$/', $cellphone)) {

return false;

}

 

return true;

}

 

 

if (check_email_address($email) && check_phone_number($cellphone)) {

 

 

 

 

$message = <<<MESSAGE

<html>

  <head>

    <title>Contact Form</title>

  </head>

  <body>

    <table style="font:Arial, Helvetica, sans-serif" style="font-size:12px" border="0" cellspacing="3" align="center">

      <tr>

        <td align="center" bgcolor="#999999" colspan="2"><strong>CONTACT FORM</strong></td>

  </tr>

   <tr>

        <td bgcolor="#FFFFFF" colspan="2"></td>

  </tr>

   <tr>

        <td bgcolor="#999999" colspan="2"><strong>$enquiry</strong></td>

  </tr>

  <tr>

        <td bgcolor="#999999" width="150">Name</td>

        <td bgcolor="#cccccc" width="300">$name</td>

      </tr>

  <tr>

        <td bgcolor="#999999" width="150">Cell Number</td>

        <td bgcolor="#cccccc" width="300">$cellphone</td>

      </tr>

  <tr>

        <td bgcolor="#999999" width="150">Company</td>

        <td bgcolor="#cccccc" width="300">$company</td>

      </tr>

   <tr>

        <td bgcolor="#999999" width="150">Position</td>

        <td bgcolor="#cccccc" width="300">$position</td>

      </tr>

    </table>

  </body>

</html>

MESSAGE;

 

$subject = 'CONTACT FORM';

 

$headers  = 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'From: ' . $email;

 

 

mail($to, $subject, $message, $headers);

header('Location: http://www.yellowwoodestates.co.za/confirmed.html');

 

 

}else

 

{

   echo("<HTML><BODY>Sorry, this does not appear to be a valid email address! Please go back and try again.");

   echo("</BODY></HTML>");

}

 

?>

 

 

 

 

 

 

HTML SCRIPT

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

 

<TABLE cellSpacing=0 cellPadding=0 width=350 border=0>

  <TBODY>

  <TR>

    <TD>

      <FORM id="theForm" name="theForm" action="sendmail1.php" method="post">

      <TABLE cellSpacing=0 cellPadding=0 width=400 align=left border=0><!--DWLayoutTable-->

        <TBODY>

        <TR>

          <TD width=118 height=17></TD>

          <TD vAlign=top width=257>

            <DIV align=left><SELECT class="formstyle1" id="enquiry-type" size="1"

            name="enquiry-type"> <OPTION value="Add me to the mailing list"

              selected>Add me to the mailing list</OPTION> <OPTION

              value="I'm a potential Purchaser">I'm a potential

              Purchaser</OPTION> <OPTION

              value="I'm an existing buyer in Yellowwood">I'm an existing buyer

              in Yellowwood</OPTION></SELECT> </DIV></TD>

          <TD width=4></TD></TR>

        <TR>

          <TD height=20></TD>

          <TD> </TD>

          <TD></TD></TR>

        <TR>

          <TD vAlign=top height=21>

            <DIV align=left><SPAN class=TableList>Name:</SPAN> </DIV></TD>

          <TD vAlign=top>

            <DIV align=left><INPUT class="formstyle1" id="name" maxLength="50" size="32"

            name="name"> <SPAN class=headlineBrown>*</SPAN> </DIV></TD>

          <TD></TD></TR>

        <TR>

          <TD colSpan=2 height=3></TD>

          <TD></TD></TR>

        <TR>

          <TD vAlign=top height=21>

            <DIV align=left><SPAN class=TableList>Cell Number:</SPAN>          </DIV></TD>

          <TD vAlign=top>

            <DIV align=left><INPUT class="formstyle1" id="cellphone" maxLength="11"

            size="32" name="cellphone">

            *</DIV></TD>

          <TD></TD></TR>

        <TR>

          <TD colSpan=2 height=3></TD>

          <TD></TD></TR>

        <TR>

          <TD vAlign=top height=21>

            <DIV align=left><SPAN class=TableList>Email Address:</SPAN>            </DIV></TD>

          <TD vAlign=top>

            <DIV align=left><INPUT class="formstyle1" id="email" maxLength="50"

            size="32" name="email">

            *</DIV></TD>

          <TD></TD></TR>

        <TR>

          <TD vAlign=top colSpan=2 height=3></TD>

          <TD></TD></TR>

        <TR>

          <TD vAlign=top height=17>

            <DIV align=left><SPAN class=TableList>Company:</SPAN> </DIV></TD>

          <TD vAlign=top>

            <DIV align=left><INPUT class="formstyle1" id="company" maxLength="50"

            size="32" name="company"> </DIV></TD>

          <TD></TD></TR>

        <TR>

          <TD vAlign=top colSpan=2 height=3></TD>

          <TD></TD></TR>

        <TR>

          <TD vAlign=top height=17>

            <DIV align=left><SPAN class=TableList>Position:</SPAN></DIV></TD>

          <TD vAlign=top>

            <DIV align=left><INPUT class="formstyle1" id="position" maxLength="50"

            size="32" name="position"> </DIV></TD>

          <TD></TD></TR>

        <TR>

          <TD height=10></TD>

          <TD></TD>

          <TD></TD></TR>

        <TR>

          <TD vAlign=top height=25><!--DWLayoutEmptyCell--> </TD>

          <TD vAlign=top>

            <DIV align=left><FONT face="Verdana, Arial, Helvetica" color=#000000

            size=1><INPUT class="buttonstyle11111" type="submit" value="Submit" name="submit">

            </FONT></DIV></TD>

          <TD></TD></TR></TBODY></TABLE></FORM></TD></TR></TBODY></TABLE>

              </div></td>

        </tr>

      </table>

 

</body>

</html>

 

 

 

Link to comment
Share on other sites

Well this script below is suposed to validate the cell phone number  or 10 characters. I need it to come up with an error if the number is more than 10 and less than 10 numbers.

 

function check_phone_number($cellphone) {

 

$cellphone = preg_replace('/[^0-9]/', '', $cellphone);

 

 

if (preg_match('/^1?[0-9]{10}$/', $cellphone)) {

return false;

}

 

return true;

}

 

 

 

Link to comment
Share on other sites

<?php function check_phone_number($cellphone) {

if(trim($cellphone) == 10) { return true;  }

else {  return false; }

}
?>

 

This will make sure that the phone number has to be 10 characters. The trim function removes any white spaces...

 

Sorry i can not do this for you using regular expressions but im sure someone else could if you ask

Link to comment
Share on other sites

<?php function check_phone_number($cellphone) {

if(trim($cellphone) == 10) { return true;  }

else {  return false; }

}
?>

 

This will make sure that the phone number has to be 10 characters. The trim function removes any white spaces...

 

Sorry i can not do this for you using regular expressions but im sure someone else could if you ask

 

This wouldn't check to see if the phnone number is 10 characters, it would check to see if it was equal to 10.

 

if(strlen(trim($cellphone)) == 10) { return true; }

 

Link to comment
Share on other sites

yer your right... everything seems to be hard work today..

 

strlen

<?php function check_phone_number($cellphone) {

$cellphone_length = trim(strlen($cellphone));

if($cellphone_length) == 10) { return true;  }

else {  return false; }

}
?>

Link to comment
Share on other sites

Thank YOU, thanks fantastic! I got another question, if i wanted my error messages to display on the form page instead of a new page, how would i do that? I wouldn't even know what search for to find that. So for example, my email error message would display Invalid next to the input box.

 

Thanks again hey

 

Link to comment
Share on other sites

You would need to submit the form to itself, or use JavaScript and Ajax.

 

Simple normal example:

<?php
if(isset($_POST['submit'])) {
  if(empty($_POST['name'])) {
    print 'Please enter a name!';
  }
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  <input type="text" name="name" />
  <input type="submit" name="submit" />
</form>

Link to comment
Share on other sites

Change this

 

<?php function check_phone_number($cellphone) {

$cellphone_length = trim(strlen($cellphone));

if($cellphone_length) == 10) { return true;  }

else {  return false; }

}
?>

 

to this

 


<?php
$pattern = '/^[0-9]{10,10}$/';
$cellphone = "0000000000";

#
if (preg_match($pattern,$cellphone)) { return true; }
#
else { return false; }

?>

 

The new code should only allow 10 numbers where as the old code would allow 10 characters meaning someone could have put letters in as there telephone number.

 

 

 

Link to comment
Share on other sites

Hey Ben i got this error msg:

 

Warning: preg_match() [function.preg-match]: Empty regular expression in /usr/www/users/yellowf/sendmail1.php on line 52

 

Line 52:  if (preg_match($pattern,$cellphone)) { return true; }

 

Thanks Wolphie, so that means i can put the error messages within the form page?

 

Is there such a command that i can put in that php script within the form page to go to sendmail.php to actually send the form mailer?

Link to comment
Share on other sites

it shouldn't have done did you put it inside your function like so

 

<?php
function check_phone_number($cellphone) {
$pattern = '/^[0-9]{10,10}$/';
$cellphone = "00000d00000";//remove this in live version just for testing

#
if (preg_match($pattern,$cellphone)) { return true; }
#
else { return false; }

}


?>

 

As for sending the mail you can use the mail function just look here to learn how to do it then post back with any other problems

 

http://au.php.net/function.mail

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.