I am trying to get my code to work for a form. I want it to validate a phone number. It want to continue to tell me the phone number is not correct even when it is.
Here's what I have. I took out the rest of the code that pertain to this part to simplify it. I am not sure if I have the code right but I am looking to have someone only be able to type number. Enter a 10 digit string of number and have it change to the following format (XXX) XXX-XXXX if this is possible.
<?php
include_once "contact-config.php";
$error_message = '';
if (!isset($_POST['submit'])) {
  showForm();
} else { //form submitted
  $error = 0;
 if(!empty($_POST['phone'])) {
$phone[2] = clean_var($_POST['phone']);
if (!validPhone($phone[2])) {
  $error = 1;
  $phone[3] = 'color:#FF0000;';
  $phone[4] = '<strong><span style="color:#FF0000;">Invalid Phone Number</span></strong>';
  }
 }
  else {
    $error = 1;
    $phone[3] = 'color:#FF0000;';
  } 
<td class="quote_text" style="width:{$left_col_width}; text-align:right; vertical-align:top; padding:{$cell_padding}; {$phone[3]}"><span class='required'><b>* </b></span>{$phone[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$phone[1]}" value="{$phone[2]}" size="20" maxlength="11" id="{$phone[1]}" /> {$phone[4]}</td>
/* Phone Number Validation Function. */
function validPhone($phone)
{
  $isValid = true;
  if(array_key_exists('phone', $_POST))
  {
    if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $_POST['phone']))
    {
      $isValid = false;
    }
 }
 return $isValid;
 }
?>
 
     
    