Hi there -
I’m working on a “Join our Mailing List” type form and this is the most complicated PHP form validation I’ve ever put together, even though I know there is probably a much simpler and more elegant solution.
I’m using javascript to validate client-side for mandatory fields, and then once the form is submitted I need to validate all submitted fields server-side. Right now, the field “Phone_number” is NOT a mandatory field, but my form will not validate (server-side) if the phone field is left empty.
I attempted a simple if (isset($phone) solution to make the phone field validate ONLY if there is an entry there but it hasn’t helped. This is the limit of my capabilities right now.
Any help or suggestions greatly appreciated. Please let me know if you need more info.
Here is my code:
<?php if(isset($_POST['submit'])) { // where to send form to $email_to = "[email protected]"; $email_subject = "Add me to your mailing list! "; function died($error) { // your error code can go here echo "Whoa, crazy white screen!"; echo "You're here because there were error(s) found with the form you submitted. "; echo "These errors appear below.
"; echo $error."
"; echo "Please hit your browser's back button and fix these errors.
"; die(); } // validation expected data exists if(!isset($_POST['First_name']) || !isset($_POST['Last_name'])) { died('We are sorry, try typing in your name again.'); } $emailcheck = $_POST['emailCheckbox' == true]; // required $mailcheck = $_POST['mailCheckbox' == true]; // required $first_name = $_POST['First_name']; // required $last_name = $_POST['Last_name']; // required $email_from = $_POST['Email_address']; // required $address = $_POST['Address']; // required $city = $_POST['City']; // required $state = $_POST['State']; // required $zip = $_POST['Zip']; // required $phone = $_POST['Phone_number']; $how = $_POST['How_did_you_hear_about_us']; $error_message = ""; //create validation regex's $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; $string_exp = "^[a-z .'-]+$"; $phone_exp = "[^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$]"; $add_exp = "^[[:alnum:]][a-zA-Z0-9_\.\#\' \-]{2,60}$"; //check which var's to validate if(isset($_POST['emailCheckbox'])) { if(!eregi($email_exp,$email_from)) { $error_message .= 'The email address you entered does not appear to be valid.
'; } if(!eregi($string_exp,$first_name)) { $error_message .= 'Oops! Check your first name.
'; } if(!eregi($string_exp,$last_name)) { $error_message .= 'Oops! Check your last name.
'; } } if (isset($_POST['mailCheckbox'])) { if(!eregi($string_exp,$first_name)) { $error_message .= 'Oops! Check your first name.
'; } if(!eregi($string_exp,$last_name)) { $error_message .= 'Oops! Check your last name.
'; } if(!eregi($add_exp,$address)) { $error_message .= 'Your address is not valid.
'; } if(!eregi($string_exp,$city)) { $error_message .= 'Your city is not valid.
'; } if(!eregi($string_exp,$state)) { $error_message .= 'Your state is not valid.
'; } if(!preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zip)) { $error_message .= 'Oops! Your zip code is not valid.
'; } } if (isset($phone)) { if(!eregi($phone_exp,$phone) ) { $error_message .= 'Oops! Your phone number is not valid.
'; } } if(!eregi($string_exp,$how)) { $error_message .= 'The message you entered does not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Address: ".clean_string($address)."\n"; $email_message .= "City: ".clean_string($city)."\n"; $email_message .= "State: ".clean_string($state)."\n"; $email_message .= "Zip: ".clean_string($zip)."\n"; $email_message .= "Phone: ".clean_string($phone)."\n"; $email_message .= "How did you hear about us: ".clean_string($how)."\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); if (!$error_message) { header ('Location: thanks.html'); exit(); } } ?>