PHP联系人表单电话验证号码的正确数量



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

你好,

我有一个php表单,它在提交后验证内容。粘性php表单就是它的名称。当发现错误时,它会将用户数据保存在输入框中,这样用户就不必再次输入所有数据。

当提交电话号码时,我需要它来验证第一个输入框中是否有3个字符/数字,然后在下一个框中有3个,最后一个框是4个。现在的方式是,只要你在第一个输入框中输入数字,它就会在其他输入框中查找电话号码。因此,我希望在验证过程中添加一个最小字符/数字脚本。我现在有一个表格来验证它是一个数字。我还需要它来验证手机的每个输入框中的数字数量是否正确。我相信这只是将elseif语句改为just-if内部的另一个if,但这也不起作用。如有任何帮助,我们将不胜感激。艺术学院只教了这么多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 -->

使用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;
}

上面的代码只是检查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; 
}

最新更新