PHP联系表单电话验证正确的数字量

时间:2011-06-10 20:28:26

标签: php

PHP联系表格电话验证正确的数字

您好,

我有这个PHP表单验证了内容一旦提交了一个粘性的PHP表单就是它的名字。如果发现错误,它会将用户数据保留在输入框中,这样用户就不必再次重新输入所有数据。

当提交电话号码时,我需要它来验证第一个输入框中有3个字符/数字,然后是最后一个输入框中的3个字符/数字。 它现在的方式只要你在第一个输入框中输入数字就可以看到电话号码的其余输入框。所以我希望在验证过程中添加最小字符/数字脚本。我有表格验证它是一个数字。我还需要它来验证手机的每个输入框中是否有正确数量的数字。我相信这只是将elseif语句改为仅在另一个内部,但是也没有用。任何帮助将非常感激。艺术学院只用PHP教授这么多,而不是这个。

这是验证电话号码的脚本的特定区域:

//validate the phone number
if(is_numeric($_POST['phone01'])) { 
        $phone = $_POST['phone01']. '-';

}elseif(is_numeric($_POST['phone02'])) {    
        $phone .= $_POST['phone02']. '-';   

}elseif(is_numeric($_POST['phone03'])) { 
        $phone .= $_POST['phone03'];
}else{  
    print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
    $validate = FALSE;
}

这是表单本身的整个脚本的副本:

<?php 
// This page receives the data from itself and validates as well

//error reporting!
ini_set ('display_errors', 1);

//Shows all possible problem!
error_reporting (E_ALL);

// validate email  
function isValidEmail($email){
    return eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $email);
}


//show form
function show_form($firstName='',$lastName='',$businessName='',$email='',$phone01='',$phone02='',$phone03='',$message=''){

?>

<!--The form starts here --> 

      <form action ="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post" name="contact form" target="_self" id="contact form" dir="ltr" >

        <table bgcolor="#000000" width="525" border="0" align="center">
          <tr>
            <td width="25%" align="right">*First Name:</td>
            <td colspan="2" align="left"><input name="firstName" type="text" id="firstName" tabindex="1" size="30" value="<?php if(isset($_POST['firstName'])) { print htmlspecialchars($_POST['firstName']); }?>"/></td>
            </tr>
          <tr>
            <td align="right">*Last Name:</td>
            <td colspan="2" align="left"><input name="lastName" type="text" id="lastName" tabindex="2" size="30" value="<?php if(isset($_POST['lastName'])) {print htmlspecialchars($_POST['lastName']); }?>"/></td>
            </tr>
          <tr>
            <td align="right">Business Name:</td>
            <td colspan="2" align="left"><input name="businessName" type="text" id="businessName" tabindex="3" size="35" value="<?php if(isset($_POST['businessName'])) {print htmlspecialchars($_POST['businessName']); }?>"/></td>
            </tr>
          <tr>
            <td align="right">*Email: </td>
            <td colspan="2" align="left"><input name="email" type="text" id="email" tabindex="4" size="35" value="<?php if(isset($_POST['email'])) {print htmlspecialchars($_POST['email']); }?>"/></td>
            </tr>
          <tr>
            <td align="right">*Phone Number:</td>
            <td colspan="2" align="left">
              <input name="phone01" type="text" id="phone01" size="3" maxlength="3" tabindex="5"value="<?php if(isset($_POST['phone01'])) {print htmlspecialchars($_POST['phone01']); }?>"/>
              - <input name="phone02" type="text" id="phone02" size="3" maxlength="3" tabindex="6"value="<?php if(isset($_POST['phone02'])) {print htmlspecialchars($_POST['phone02']); }?>"/>
              - <input name="phone03" type="text" id="phone03" size="4" maxlength="4" tabindex="7" value="<?php if(isset($_POST['phone03'])) {print htmlspecialchars($_POST['phone03']); }?>"/></td>
            </tr>
          <tr align="center">
            <td align="right">*Message:</td>
            <td colspan="2" align="left"><textarea name="message" type="text" id="message" tabindex="8" cols="45" rows="4"><?php if(isset($_POST['message'])) {print htmlspecialchars($_POST['message']); }?></textarea>
            </td>
            </tr>
          <tr align="center">
            <td>&nbsp;</td>
            <td><input name="submit" type="submit" tabindex="9" value="Email" /></td>
            <td><input type="reset" name="reset" id="reset" value=" Reset " tabindex="10"/></td>
          </tr>
          </table>
      </form> 
<?php 
} // end of show_form function
$validate = TRUE;


if($_SERVER['REQUEST_METHOD']!='POST') {

  show_form();

  } else {

    //validate form fields

    //validate the first name
    if(empty($_POST['firstName'])) {
            print '<p class="error">Please enter your First Name.</p>';
            $validate = FALSE;
    }

    //validate the last name
    if(empty($_POST['lastName'])) {
            print '<p class="error">Please enter your Last Name.</p>';
            $validate = FALSE;
    }

    //validate the enail with email arrary
    if(!isValidEmail($_POST['email'])) {
            print '<p class="error">Please enter your Email Address in the correct formate.</p>';
            $validate = FALSE;
    }

    //validate the phone number
    if(is_numeric($_POST['phone01'])) { 
            $phone = $_POST['phone01']. '-';

    }elseif(is_numeric($_POST['phone02'])) {    
            $phone .= $_POST['phone02']. '-';   

    }elseif(is_numeric($_POST['phone03'])) { 
            $phone .= $_POST['phone03'];
    }else{  
        print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
        $validate = FALSE;
    }

    //validate the message
    if(empty($_POST['message'])) {
            print '<p class="error">Please enter your Messagee.</p>';
            $validate = FALSE;
    }

    if(!$validate){
        print "<p>Please fill in all the fields with an asterisk * next to it and than please try again!</p>";  

        show_form($_POST['firstName'],$_POST['lastName'],$_POST['businessName'],$_POST['email'],$_POST['phone01'],$_POST['phone02'],$_POST['phone03'],$_POST['message']);

    }else{
$phone01 = $_POST['phone01'];
$phone02 = $_POST['phone02'];
$phone03 = $_POST['phone03'];       
$phone = $phone01.'-'.$phone02.'-'.$phone03;        

    //confirmation email to client includes all information provided
    mail($_POST['email'], 'Contact Confirmation from www.Ozbar.net Web site', 'Thank You '.$_POST['firstName'].' '.$_POST['lastName'].' for your request for us to contact you. 

    Below is the information your provided us to contact you per your request.
    First Name: '.$_POST['firstName'].' 
    Last Name: '.$_POST['lastName'].'
    Business Name:  '.$_POST['businessName'].'
    Email Address: '.$_POST['email'].'
    Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].' 
    Message: '.$_POST['message'].' ','From:contact@steveoatman.me); 

    //notice of a new contact request
    mail('contact@steveoatman.me, 'Contact Request from www.Steveoatman.me Web site', ' 
    First Name: '.$_POST['firstName'].' 
    Last Word: '.$_POST['lastName'].'
    Business Name:  '.$_POST['businessName'].'
    Email Address: '.$_POST['email'].'
    Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].' 
    Message: '.$_POST['message'].' ','From:contact@steveoatman.me);

    print '<p align="center">Thank You For Your Request!</p>'?><br /><?php
    print '<p align="center">We will contact you back with in 24-48 hours.</p>' 
?>
<br /><br /> <!--  if all validated a thank you statement -->
<?php
}

} //end of IF submit
// end of all php
?> 


<!-- end of #ref form -->

2 个答案:

答案 0 :(得分:0)

上面的代码只是检查3个字段中的任何是否有一个数字,而不是所有

要实现上述目标,这样的事情就可以做到:

if (is_numeric($_POST['phone01']) && is_numeric($_POST['phone02']) && is_numeric($_POST['phone03']))
{
     $phone = $_POST['phone01']."-".$_POST['phone02']."-".$_POST['phone03'];
}
else
{
    print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
    $validate = FALSE;
}

但是,上述代码不进行任何其他类型的验证,例如检查每个表单字段中是否已放入所需的位数。 您可能还想使用'ctype_digit()'函数来确保只输入数字,而不是1.3等数字字符串。

所以你可以做类似

的事情
if (!ctype_digit($_POST['phone01']) || strlen($_POST['phone01']) != 4) 
{ 
    $validate = FALSE; 
}

答案 1 :(得分:0)

使用strlen验证字段长度。不要使用if/elseif,因为您要验证所有三个输入。设置一个标志以跟踪电话号码的有效性。

$invalid_phone = false;
if((strlen($_POST['phone01']) == 3) && is_numeric($_POST['phone01'])) { 
        $phone = $_POST['phone01']. '-';
}else{
   $invalid_phone = true;
}

if((strlen($_POST['phone02']) == 3) && is_numeric($_POST['phone02'])) {    
        $phone .= $_POST['phone02']. '-';   
}else{
   $invalid_phone = true;
}

if((strlen($_POST['phone03']) == 4) && is_numeric($_POST['phone03'])) { 
        $phone .= $_POST['phone03'];
}else{
   $invalid_phone = true;
}

if($invalid_phone){
    print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
    $validate = FALSE;
}