jay4708 Posted April 3, 2010 Share Posted April 3, 2010 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\">"; } ?> Quote Link to comment Share on other sites More sharing options...
litebearer Posted April 4, 2010 Share Posted April 4, 2010 Being terribly lazy, I prefer the use of separate fields for - area code / prefix / last four digits. Makes check for only numbers and a FULL phone number easier for me. Just a thought... Quote Link to comment Share on other sites More sharing options...
Zane Posted April 4, 2010 Share Posted April 4, 2010 IMHO.. it'd be better to use regex. You could probably scan the current questions in the PHP Regex board already and find the solution you need. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted April 4, 2010 Share Posted April 4, 2010 Seeing as you want just numbers it easy to remove other characters that might be in the number. $phone = str_replace(array('(',')','-',' '), '',$_POST['phone']); HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 4, 2010 Share Posted April 4, 2010 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. Quote Link to comment 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.