Please, need help with php email form

Hello:

I am just a beginner and having a hard time figuring out how to validate the phone number.
Here is my entire code. I hope someone could, please, help me out!

<?php if(isset($_POST['sendContact'])){ // ENTER YOUR EMAIL HERE $to_email = "[email protected]";// $hasError = 'false'; if(trim($_POST['fullname']) == '') { $hasError = "true"; echo '
Please enter your name.
'; exit; } else { $name = trim($_POST['fullname']); } if(trim($_POST['email']) == '') { $hasError = "true"; echo '
Please enter your valid email address.
'; exit; } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { $hasError = "true"; echo '
Please enter your valid email address.
'; exit; } else { $email = trim($_POST['email']); } if(trim($_POST['form_message']) == '') { $hasError = "true"; echo '
Please enter your message.
'; exit; } else { $comment = stripslashes(trim($_POST['form_message'])); } if(trim($_POST['phone']) == '') { $hasError = "true"; echo '
Please enter your valid phone number.
'; exit; } else if (!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/", trim($_POST['phone']))) { $hasError = "true"; echo '
Please enter your valid phone number.
'; exit; } else { $phone = trim($_POST['phone']); } if($hasError!="true") { $e_date = date( 'Y/m/d - h:i A', time() ); $e_subject = 'New Message By ' . $name . ' on ' . $e_date . ''; $e_body = "$name has contacted you.\r\n\n"; $e_body .= "Comment: $comment \r\n\n"; $e_body .= "Email: $email \r\n\n"; $e_body .= "Phone: $phone \r\n\n"; $msg = $e_body; mail( $to_email, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n" ); echo ""; echo "
"; echo "

Message Sent Successfully.

"; echo "

Thank you $name, your message has been submitted to us.

"; echo "
"; exit; } } ?>

What I might start by doing is instead of have just one field have three field like 111-222-3333
You first check each of the field and AND them together so if the first two fields do not == a strlen of 3 and the third field a strlen of 4 then have error message for that. then on each field make sure they are numbers

Example I got from the php.net documents

[php]<?php
$values = array(23, “23”, 23.5, “23.5”, null, true, false);
foreach ($values as $value) {
echo “is_int(”;
var_export($value);
echo ") = ";
var_dump(is_int($value));
}
?>
[/php]

The above example will output:

is_int(23) = bool(true)
is_int(‘23’) = bool(false)
is_int(23.5) = bool(false)
is_int(‘23.5’) = bool(false)
is_int(NULL) = bool(false)
is_int(true) = bool(false)
is_int(false) = bool(false)

hope this helps a little

Sponsor our Newsletter | Privacy Policy | Terms of Service