PHP 手机模式验证错误


PHP phone pattern validation error

>我有一个inc文件(test_form4.inc),它定义了收集用户名和电话号码的表单。

<!doctype html>
<?php
 /*  Program name: form_test4.inc
  *  Description:  Defines a form that collects a user's 
  *                name and phone number.
  */
$labels = array( "first_name" => "First Name",
                 "middle_name" => "Middle Name",
                 "last_name" => "Last Name",
                 "phone" => "Phone");
$radios = array( "New", "Changed");
$submit = "Submit Phone Number";
?>
<html>
<head>
<title>Form 2</title>
  <style type='text/css'>
   <!--
      form {
        margin: 1.5em 0 0 0;
        padding: 0;
      }
      .field {padding-bottom: 1em;}
      label {
        font-weight: bold;
        float: left;
        width: 20%;
        margin-right: 1em;
        text-align: right;
      }
      .submit {
        margin-left: 35%;
      }
   -->
 </style>
</head>
<body>
<h3>Please enter your phone number below</h3>
<?php
 /* loop that displays the form */
  echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
  foreach($labels as $field => $label)
  {
    echo "<div class='field'><label for='$field'>$label</label>
          <input id='$field' name='$field' type='text' value='".@$$field."'
           size='50%' maxlength='65' /></div>'n";
  }
  echo "<div class='field'>
          <input type='radio' name='status' checked='checked'
            value='new' style='margin-left: 25%'/>$radios[0]
          <input type='radio' name='status' 
            value='changed' style='margin-left: 1em' />$radios[1]</div>'n";
  echo  "<div><input type='hidden' name='submitted' value='yes' /></div>'n";
  echo  "<div class='submit'>
          <input type='submit' name='phoneButton' value='$submit'></div>";
?>
</form>
</body>
</html>

。以及一个 php 文件,用于检查空白或验证名为 (checkBlankOnly2.php) 的表单

<?php
/*  Program name: checkBlankOnly_2.php 
 *  Description:  Program displays the blank form and checks 
 *  all the form fields for blank fields.
 */
if(isset($_POST['submitted']) and $_POST['submitted'] == "yes")
{
  foreach($_POST as $field => $value)           
  {
    if(empty($value))
    {
      if($field != "middle_name")
      {
         $blank_array[] = $field;
      }
    } 
    else                                                    
    {
      $good_data[$field] = strip_tags(trim($value));
    }
  } 
  if(@sizeof($blank_array) > 0) 
  {
  $message = "<p style='color: red; margin-bottom: 0; 
                 font-weight: bold'>
                 You didn't fill in one or more required fields. 
                 You must enter: 
                 <ul style='color: red; margin-top: 0; 
                 list-style: none' >";
 /* display list of missing information */
    foreach($blank_array as $value)
    {
       $message .= "<li>$value</li>";
    }
    $message .= "</ul>"; 
    echo $message;
    extract($good_data);
    include("form_test4.inc");  
    exit();    
  }
foreach($_POST as $field => $value)
{
  if(!empty($value))
  {
    $name_patt = "/^[A-Za-z' -]{1,50}$/";
    $phone_patt = "/^[0-9)(xX -]{7,20}$/";
    $radio_patt = "/(new|changed)/";
    if(preg_match("/name/i",$field))
    {
      if(!preg_match($name_patt,$value))
      {
        $error_array[] = "$value is not a valid name";
      }
    } 
    if(preg_match("/phone/i",$field))
    {
      if(!preg_match($phone_patt,$value))
      {
        $error_array[] = "$value is not a valid phone number";
      }
    }  // endif phone format check
    if(preg_match("/status/i",$field))
    {
      if(!preg_match($radio_patt,$value))
      {
        $error_array[] = "$value is not a valid status";
      }
    }
  } 
  $clean_data[$field] = strip_tags(trim($value));
}
if(@sizeof($error_array) > 0) 
{
  $message = "<ul style='color: red; list-style: none' >";
  foreach($error_array as $value)
  {
   $message .= "<li>$value</li>";
  }
  $message .= "</ul>"; 

  echo $message;
  extract($clean_data);
  include("form_test4.inc");
  exit();   
}
else
{
 echo "Data is all okay";
} 
}
else
{
  include("form_test4.inc");
}   
?>

无法弄清楚我的错误来自哪里,我确定问题出在电话号码上。我的课程说,电话号码preg_match是数字格式 555-5555 或 (888) 555-5555,但当我插入所有数据时,例如:名字、姓氏和电话号码这些格式,我收到错误"不是有效的电话号码"。请帮帮我,我想不通。

谢谢。

字段 phoneButton 被视为电话号码,因为它通过了您的条件if (preg_match("/phone/i",$field)),它的值"提交电话号码"然后被验证为电话号码,从而生成错误"提交电话号码不是有效的电话号码"。

将您的

"phoneButton"字段重命名为"提交按钮",您应该没问题。

替换

$phone_patt = "/^[0-9)(xX -]{7,20}$/";

$phone_patt = "/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/";