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;
}
}
?>