2013-11-02 70 views
0

我有一个INC文件(test_form4.inc),它定义了一个收集用户的姓名和电话号码的形式。PHP电话模式验证错误

<!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='"[email protected]$$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> 

,而哪些检查空格或验证调用(checkBlankOnly2.php)形式的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,当我插入所有数据时,例如:这些格式的名字,姓氏和电话号码我收到错误“not一个有效的电话号码“。 请帮助我,我无法弄清楚。

谢谢。

+0

为什么你的'sizeof'函数中使用错误抑制('@')?不使用它通常是一个好主意。 –

+0

php脚本不是程序.. – Joren

回答

0

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

你的“phoneButton”字段重命名,例如,“提交按钮”,你应该罚款。

-1

更换

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

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