I was using some pre-existing open source code and wanted to add a telephone validation to a form. The original code did not have a telephone validation. I tried to mirror the email php validation but modifry the preg-match for telephone digits, spaces, brackets and hyphens. The form still works fine except that it is not validating the telephone section. From testing the form, I can confirm that the telephone addition to the form is not being recognized (I know this because I tried copying the email validation into the telephone code and it did not recognize that either). My code is below. I am certainly a beginner with php and really only use php for handling the emailing of forms. Any guidance is appreciated. I am hoping to achieve the telephone validating correctly when the form is submitted.
Code:
<?php
// Set email variables
$email_to = '[email protected]';
$email_subject = 'New email from form';
// Set required fields
$required_fields = array('firstName','lastName','telephone','email','comment');
// set error messages
$error_messages = array(
'firstName' => 'Please enter your first name to proceed.',
'lastName' => 'Please enter your last name to proceed.',
'email' => 'Please enter a valid email address to proceed.',
'telephone' => 'Please enter a valid telephone number to proceed.',
'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
// validate the telephone supplied
if($field == 'telephone') if(!validate_telephone($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// simple telephone content
foreach($_POST as $key => $value) {
if($key != 'submit') $telephone_content .= $key . ':' . $value . "/n";
}
// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
function validate_telephone($telephone = FALSE) {
return (preg_match('/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/', $telephone))? TRUE : FALSE;
}
?>
I have tried copying the code from the email validation and changing the variables to $telephone. I then took the preg_match validaiton code and replaced it with a telephone validation code ('/^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/'). When the form is submitted, any text you add to the telephone form box is accepted and emailed.
The expecation I had was that the form would return a message in the form stating "Please enter a valid telephone number to proceed."
1, even though I’m US and usually filling out forms in the US. Third, I have an extension which I want people to call.