Jump to content

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/217137-email-validation/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127718
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127725
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127728
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127736
Share on other sites

<?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.

Link to comment
https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127738
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217137-email-validation/#findComment-1127739
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.