Hi guys I got a form and it works it send the data needed to the email address required, but I am having trouble getting the validation part of it working as needed. Its allowing no entery for phone number, I would also like some advise to prevent people entering 00000 000000 so people have to enter what is more likely to be a real phone number. Any help/advise on this would be great cheers. <?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "my email here"; $email_subject = "email subject here"; function died($error) { // your error code can go here echo "<br /><p>We are very sorry, but there were errors found with the form you submitted. "; echo "These errors appear below.</p>"; echo "<p> ".$error."</p>"; echo "<p>Please go back and fix these errors.</p>"; echo "<p><a href='#' onClick='history.go(-1)'><img src='img/green-arrow.png' alt='Green Back Arrow' /></a></p>"; die(); } // validation expected data exists if(!isset($_POST['name']) || !isset($_POST['phone']) || !isset($_POST['mobile']) || !isset($_POST['email']) || !isset($_POST['vehicle']) || !isset($_POST['term'])) { died('We are sorry, but there appears to be a problem with the form your submitted.'); } $name = $_POST['name']; // required $company_name = $_POST['company_name']; // not required $telephone = $_POST['phone']; // required $mobile = $_POST['mobile']; // required $email_from = $_POST['email']; // required $vehicle = $_POST['vehicle']; // required $term = $_POST['term']; // required $error_message = ""; $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; if(!eregi($email_exp,$email_from)) { $error_message .= '* The Email Address you entered does not appear to be valid.<br />'; } $string_exp = "^[a-z .'-]+$"; if(!eregi($string_exp,$name)) { $error_message .= '* The Name you entered does not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Contact Details.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Company Name: ".clean_string($company_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Mobile: ".clean_string($mobile)."\n"; $email_message .= "Vehicle: ".clean_string($vehicle)."\n"; $email_message .= "Term: ".clean_string($term)." Months\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <!-- include your own success html here --> <p> </p> <h2>Thank you for contacting us for your quote.<br /> We will be in touch with you very soon.</h2> <? } ?> PHP:
There's bound to be a regex for it but be careful if your site is international... I get fed up with sites that don't accept my phone number unless I add the country code and area code to make it resemble an american phone number.
Awsome I have it working so both numbers have to be valid, how would I code it to say one or the other has to be valid? So if they put in their mainland number they dont have to put in mobile but otherwise they will have to enter mobile numer and vise versa.
@johneva <?php function died($error) { // your error code can go here echo "<br /><p>We are very sorry, but there were errors found with the form you submitted. "; echo "These errors appear below.</p>"; echo "<p> " . $error . "</p>"; echo "<p>Please go back and fix these errors.</p>"; echo "<p><a href='#' onClick='history.go(-1)'><img src='img/green-arrow.png' alt='Green Back Arrow' /></a></p>"; die(); } function clean_string($string) { $bad = array("content-type", "bcc:", "to:", "cc:", "href"); return str_replace($bad, "", $string); } if (isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "my email here"; $email_subject = "email subject here"; // validation expected data exists if (!isset($_POST['name']) || !isset($_POST['phone']) || !isset($_POST['mobile']) || !isset($_POST['email']) || !isset($_POST['vehicle']) || !isset($_POST['term'])) { died('We are sorry, but there appears to be a problem with the form your submitted.'); } //sanitization hack.. $_POST = array_map("strip_tags", $_POST); // required $name = $_POST['name']; // not required $company_name = $_POST['company_name']; // required $telephone = $_POST['phone']; // required $mobile = $_POST['mobile']; // required $email_from = $_POST['email']; // required $vehicle = $_POST['vehicle']; // required $term = $_POST['term']; $error_message = ""; $email_exp = "~^[A-Z0-9\._%\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,4}$~i"; if (!preg_match($email_exp, $email_from)) { $error_message .= '* The Email Address you entered does not appear to be valid.<br />'; } $string_exp = "~^[a-z .'-]+$~"; if (!preg_match($string_exp, $name)) { $error_message .= '* The Name you entered does not appear to be valid.<br />'; } $phone_exp = "~^([0-9]{11})$~"; if(!preg_match($phone_exp , $mobile) && !preg_match($phone_exp, $telephone)){ $error_message .= '* You have not entered a mobile and/or telephone number.<br />'; } if (strlen($error_message) > 0) { died($error_message); } $email_message = "Contact Details.\n\n"; $email_message .= "Name: " . clean_string($name) . "\n"; $email_message .= "Company Name: " . clean_string($company_name) . "\n"; $email_message .= "Email: " . clean_string($email_from) . "\n"; $email_message .= "Telephone: " . clean_string($telephone) . "\n"; $email_message .= "Mobile: " . clean_string($mobile) . "\n"; $email_message .= "Vehicle: " . clean_string($vehicle) . "\n"; $email_message .= "Term: " . clean_string($term) . " Months\n"; // create email headers $headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email_from . "\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <!-- include your own success html here --> <p> </p> <h2>Thank you for contacting us for your quote.<br /> We will be in touch with you very soon.</h2> <?php } ?> PHP: Also cleaned up your code alittle