Jump to content

Help validate with spaces or dashes


jay4708

Recommended Posts

Hi

 

I've created a contact form with name, email, telephone and message fields. I've validated the fields so if they are blank it returns an error. I also included in the validation a check to see if the telephone field was numbers and not other characters (if (!is_numeric($Telephone)) $validationOK=false;). It all works but.....

 

If you enter a space or dash in with the telephone number it fails validation.

 

The code is below, can somebody please help me tweak it so I can accept spaces or dashes in the telephone field.

 

Thanks

Jay

 

<?php

// get posted data
$EmailTo = "email@website.co.uk";
$Subject = "Contact form message";

$Name = Trim(stripslashes($_POST['Name'])); 
$Telephone = Trim(stripslashes($_POST['Telephone'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation, check for required fields
$validationOK=true;
if (Trim($Name)=="") $validationOK=false; 
if (Trim($Telephone)=="") $validationOK=false;
if (!is_numeric($Telephone)) $validationOK=false;
if (Trim($Email)=="") $validationOK=false;
if (Trim($Message)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n"; 
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$Email>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Link to comment
Share on other sites

Seeing as you want just numbers it easy to remove other characters that might be in the number.

$phone = str_replace(array('(',')','-',' '), '',$_POST['phone']);

 

I agreee with teamtonic that you should simply strip out the other characters and validate that you have 7 (?) numbers. Although, a regex expression would strip out ALL characters that aren't numbers. If validation fails, populate the original value in the textbox. if validation passes store just the numbers in your database.

 

$phone = preg_replace('/[^\d]/', '', $_POST['phone']);
if(strlen($phone)!=7)
{
    //validation failed
}
else
{
    //validation passed
}

 

However, you should always be very careful about manipulating user input when the user is not aware. Also, this does resrict users from entering letters. It is commonly acceptable for people to use letters in their phone numbers to represent the numbers on the keypad.

Link to comment
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.