WebProgrammerNewb22 Posted October 28, 2010 Share Posted October 28, 2010 Hey guys.. I'm completely done with my contact form data validation except I cannot seem to get one part. For my email field I need to make sure that it is a valid email address and not just words. Also, I would like to make the phone number specific length. Any ideas on how to go about this? thanks guys! <?php ini_set('display_errors', '0'); //Define Variables. $FirstName = $_GET['FirstNameTextBox']; $LastName = $_GET['LastNameTextBox']; $PhoneNumber = $_GET['PhoneNumberTextBox']; $EmailAddress = $_GET['EmailAddressTextBox']; $Address = $_GET['AddressTextBox']; $City = $_GET['CityTextBox']; $State = $_GET['StateDropDownBox']; $Zip = $_GET['ZipTextBox']; $error1='*Please enter a First Name<br>'; $error2='*Please enter a Last Name<br>'; $error3='*Please enter a Phone Number<br>'; $error4='*Please choose a state<br>'; $error5='*Please enter a valid email address<br>'; $day2 = mktime(0,0,0,date("m"),date("d")+2,date("Y")); $day3 = mktime(0,0,0,date("m"),date("d")+3,date("Y")); $day7 = mktime(0,0,0,date("m"),date("d")+7,date("Y")); // Array to collect messages $messages = array(); //Display errors. if($FirstName=="") {$messages[] = $error1; } if($LastName=="") {$messages[] = $error2; } if($PhoneNumber=="") {$messages[] = $error3; } if($State=="") {$messages[] = $error4; } if($EmailAddress=="") {$messages[] = $error5; } // Don't do this part unless we have no errors if (empty($messages)) { //Display correct contact date. if($State == "NY") { $messages[] = "Hello $FirstName $LastName! Thank you for contacting me. I will get back to you within 2 days, before " .date("d M Y", $day2); } if($State == "NJ") { $messages[] = "$Hello FirstName $LastName! Thank you for contacting me. I will get back to you within 3 days, before " .date("d M Y", $day3); } if($State == "Other") { $messages[] = "$Hello FirstName $LastName! Thank you for contacting me. I will get back to you within 1 week, before " .date("d M Y", $day7); } } // END if empty($messages echo implode('<BR>', $messages); ?> <p>--<a href="Contact_Form.html" class="style2"><span class="style1">Go Back</span></a>--</p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/ Share on other sites More sharing options...
akitchin Posted October 28, 2010 Share Posted October 28, 2010 have a look on this website for explanations of the type of regular expression pattern you'll want to use in each case: E-mail validation List of phone number and address patterns for the second link, scroll through the list to see what sort of format you want to enforce. Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127718 Share on other sites More sharing options...
Andy-H Posted October 28, 2010 Share Posted October 28, 2010 For email addresses I would use if ( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false ) { $errors[] = 'The email address you have entered is not valid.'; } else { $email = $_POST['email']; // other stuff } filter_var You may not even require regex for the phone number format, if you only wish to restrict the length use strlen and compare it's return value to the desired length. Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127725 Share on other sites More sharing options...
akitchin Posted October 28, 2010 Share Posted October 28, 2010 For email addresses I would use if ( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false ) { $errors[] = 'The email address you have entered is not valid.'; } else { $email = $_POST['email']; // other stuff } filter_var You may not even require regex for the phone number format, if you only wish to restrict the length use strlen and compare it's return value to the desired length. fancy! that's what i get for not keeping up-to-date on the newest PHP5 functions. Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127728 Share on other sites More sharing options...
WebProgrammerNewb22 Posted October 28, 2010 Author Share Posted October 28, 2010 I tried it on my page and am still getting thru without a valid email address.. Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127732 Share on other sites More sharing options...
akitchin Posted October 28, 2010 Share Posted October 28, 2010 if you just copy-pasted it without editing it to suit your script's current setup, that's not surprising. let's see what your code is currently (including the part you just inserted from Andy-H's post). Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127735 Share on other sites More sharing options...
WebProgrammerNewb22 Posted October 28, 2010 Author Share Posted October 28, 2010 <?php ini_set('display_errors', '0'); //Define Variables. $FirstName = $_GET['FirstNameTextBox']; $LastName = $_GET['LastNameTextBox']; $PhoneNumber = $_GET['PhoneNumberTextBox']; $EmailAddress = $_GET['EmailAddressTextBox']; $Address = $_GET['AddressTextBox']; $City = $_GET['CityTextBox']; $State = $_GET['StateDropDownBox']; $Zip = $_GET['ZipTextBox']; $error1='*Please enter a First Name<br>'; $error2='*Please enter a Last Name<br>'; $error3='*Please enter a Phone Number<br>'; $error4='*Please choose a state<br>'; $error5='*Please enter a valid email address<br>'; $day2 = mktime(0,0,0,date("m"),date("d")+2,date("Y")); $day3 = mktime(0,0,0,date("m"),date("d")+3,date("Y")); $day7 = mktime(0,0,0,date("m"),date("d")+7,date("Y")); // Array to collect messages $messages = array(); //Display errors. if($FirstName=="") {$messages[] = $error1; } if($LastName=="") {$messages[] = $error2; } if($PhoneNumber=="") {$messages[] = $error3; } if($State=="") {$messages[] = $error4; } if($EmailAddress=="") {$messages[] = $error5; } //validate email address if ( filter_var($_POST['EmailAddress'], FILTER_VALIDATE_EMAIL) === false ) { $errors[] = 'The email address you have entered is not valid.'; } else { $email = $_POST['email']; } // Don't do this part unless we have no errors if (empty($messages)) { //Display correct contact date. if($State == "NY") { $messages[] = "Hello $FirstName $LastName! Thank you for contacting me. I will get back to you within 2 days, before " .date("d M Y", $day2); } if($State == "NJ") { $messages[] = "$Hello FirstName $LastName! Thank you for contacting me. I will get back to you within 3 days, before " .date("d M Y", $day3); } if($State == "Other") { $messages[] = "$Hello FirstName $LastName! Thank you for contacting me. I will get back to you within 1 week, before " .date("d M Y", $day7); } } // END if empty($messages echo implode('<BR>', $messages); ?> <p>--<a href="Contact_Form.html" class="style2"><span class="style1">Go Back</span></a>--</p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127736 Share on other sites More sharing options...
Andy-H Posted October 28, 2010 Share Posted October 28, 2010 <?php ini_set('display_errors', '0'); //Define Variables. $FirstName = $_GET['FirstNameTextBox']; $LastName = $_GET['LastNameTextBox']; $PhoneNumber = $_GET['PhoneNumberTextBox']; $EmailAddress = $_GET['EmailAddressTextBox']; $Address = $_GET['AddressTextBox']; $City = $_GET['CityTextBox']; $State = $_GET['StateDropDownBox']; $Zip = $_GET['ZipTextBox']; $error1='*Please enter a First Name<br>'; $error2='*Please enter a Last Name<br>'; $error3='*Please enter a Phone Number<br>'; $error4='*Please choose a state<br>'; $error5='*Please enter a valid email address<br>'; $day2 = mktime(0,0,0,date("m"),date("d")+2,date("Y")); $day3 = mktime(0,0,0,date("m"),date("d")+3,date("Y")); $day7 = mktime(0,0,0,date("m"),date("d")+7,date("Y")); // Array to collect messages $messages = array(); //Display errors. if($FirstName=="") {$messages[] = $error1; } if($LastName=="") {$messages[] = $error2; } if($PhoneNumber=="") {$messages[] = $error3; } if($State=="") {$messages[] = $error4; } if ( filter_var($EmailAddress, FILTER_VALIDATE_EMAIL) === false ) { $messages[] = $error5; } // Don't do this part unless we have no errors if (empty($messages)) { //Display correct contact date. if($State == "NY") { $messages[] = "Hello $FirstName $LastName! Thank you for contacting me. I will get back to you within 2 days, before " .date("d M Y", $day2); } if($State == "NJ") { $messages[] = "$Hello FirstName $LastName! Thank you for contacting me. I will get back to you within 3 days, before " .date("d M Y", $day3); } if($State == "Other") { $messages[] = "$Hello FirstName $LastName! Thank you for contacting me. I will get back to you within 1 week, before " .date("d M Y", $day7); } } // END if empty($messages echo implode('<BR>', $messages); ?> <p>--<a href="Contact_Form.html" class="style2"><span class="style1">Go Back</span></a>--</p> </body> </html> lol akitchin, got it in one. Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127738 Share on other sites More sharing options...
Psycho Posted October 28, 2010 Share Posted October 28, 2010 For phone number validation I just strip out all non-numeric characters and then test the length. But, depending upon "where" your users may be from this may not be a trivial task, e.g. international users. But, for the sake of argument, let's say you expect all users to be from the US. I would first include text on the form to tell the user they must enter their full phone number, including the area code. Something such as: Phone: ____________ (xxx) xxx-xxxx Then, when processing the input, call a function such as this: function validPhone($phone) { $phone = preg_replace("/[^\d]/", '', $phone); return (strlen($phone)==10) ? $phone : false; } The function will return just the numeric digits (if only 10) or will return false, so you can perform whatever error handling is needed. Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127739 Share on other sites More sharing options...
WebProgrammerNewb22 Posted October 28, 2010 Author Share Posted October 28, 2010 I did change the variables to match mine.. what else am i doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127757 Share on other sites More sharing options...
akitchin Posted October 28, 2010 Share Posted October 28, 2010 based on the code you posted, you hadn't changed anything to match... if it's still not working (even after Andy-H's latest suggestion), can you paste what you have in your file? Quote Link to comment https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.