2011-12-25 22 views
0

嗨我從互聯網腳本得到了一個聯繫人,我一直在亂搞,唯一的問題是,我試圖添加到它的驗證不起作用。基本上它的形式是名稱,電子郵件,號碼,類型和評論。我主要的驗證問題是數字字段,我希望它,如果它是空的回聲「沒有數字」,當人鍵入字母而不是數字時,它會回顯「你需要鍵入數字」。像大聲笑。但我卡住了。你們任何一個天才都能幫助我嗎?在此先感謝下面的完整代碼。附:關於以前的帖子對不起,我不小心切斷腳本:$不能找出php的聯繫表單驗證危機

<?php 

$nowDay=date("d.m.Y"); 
$nowTime=date("H:i:s"); 
$subject = "E-mail from my site!"; 

if (isset($_POST['submit'])) { 

    //contactname 
    if (trim($_POST['name'] == '')) { 
     $hasError = true; 
    } else { 
     $name = htmlspecialchars(trim($_POST['name'])); 
    } 
    //emailaddress 
    if (trim($_POST['email'] == '')) { 
     $hasError = true; 
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i",trim($_POST['name']))) { 
     $hasError = true; 
    } else { 
     $email = htmlspecialchars(trim($_POST['email'])); 
    }    
    //phonenumber  
    if (trim($_POST['number'] == '')) 
    { 
     $fake_num = true; 
    } 
    else if(!is_numeric($_POST['phonenumber'])) 
    { 
     $fake_num = true; 
    } 
    else 
    { 
     $number = htmlspecialchars(trim($_POST['number'])); 
    } 
    //type 
    $type = trim($_POST['type']); 

    //comment  
    if (trim($_POST['comment'] == '')) { 
     $hasError = true; 
    } else { 
     $comment = htmlspecialchars(trim($_POST['comment'])); 
    }    

    if (!isset($hasError) && !isset($fake_num)) { 
     $emailTo = '[email protected]'; 
     $body = " Name: $name\n\n 
        Email: $email\n\n 
        Phone number: $number\n\n 
        Type: $type\n\n 
        Comment: $comment\n\n 
        \n This message was sent on: $nowDay at $nowTime"; 

     $headers = 'From: My Site <'.$emailTo.'>'."\r\n" .'Reply-To: '. $email; 
     mail($emailTo, $subject, $body, $headers); 
     $emailSent = true; 
    } 
} 
?> 


<?php 

if(isset($hasError)) 
{ 
    echo"<p>form has error</p>"; 
} 
?> 

<?php   
if(isset($fake_num)) 
{ 
    echo"<p>wrong num</p>"; 
} 
?> 

<?php 
if(isset($emailSent) && $emailSent == true) 
{ 
    echo "<p><strong>Email Successfully Sent!</strong></p>"; 
    echo "<p>Thank you <strong> $name </strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>"; 
} 
?> 


<div id="stylized" class="myform"> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="bookingform" id="bookingform"> 
     <h1>Booking form</h1> 
     <label> 
      Name 
      <span class="small"></span> 
     </label> 
     <input type="text" name="name" id="name" class="required"/> 
     <label> 
      Your email: 
      <span class="small"></span> 
     </label> 
     <input type="text" name="email" id="email" class="required"/> 
     <label> 
      Contact Number: 
      <span class="small"></span> 
     </label> 
     <input type="text" name="number" id="number" class="required" />              
     <label> 
      Car required: 
      <span class="small"></span> 
     </label> 
     <select name="type" id="type"> 
      <option selected="selected">Manual</option> 
      <option>Automatic</option> 
     </select> 
     <label> 
      Comment: 
      <span class="small"></span> 
     </label> 
     <textarea cols="" rows="" name="comment" id="comment" class="required"></textarea>     
     <button type="submit" name="submit">Submit</button> 
    </form> 
</div> 
+0

你已經試過了嗎? – 2011-12-25 23:04:53

+0

'ereg'中的'e'代表「邪惡」。改用['preg'](http://www.php.net/manual/en/ref.pcre.php)。 – DaveRandom 2011-12-25 23:05:51

+0

完成改成了預浸比賽 – user244228 2011-12-25 23:25:00

回答

2

用於檢查數量是否已經輸入,您可以使用:

if (!preg_match('/^?[0-9]{0,10}$/', $_POST['number'])) { 
echo "Please enter a valid number"; //Failed to meet criteria 
} 

在這裏,你還可以指定將構成數字量您的有效數字帶有大括號{0,10},在這種情況下,該數字可以長達11位數字。如果您要求它只有11位數字,則使用{11}。 如果你想檢查是否有數量都已經輸入,您可以使用:

if (empty($_POST['number'])) { 
echo "Please enter a valid number"; //Field was left empty 
} 

PHP確實有一個內置的電子郵件驗證功能,您可以使用

filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)

返回如果輸入的電子郵件格式正確,則爲true。

+0

哦~~謝謝你現在會嘗試:) – user244228 2011-12-25 23:27:55

+0

唉唉人,我把事情搞糟了,現在的IM更糊塗了:(謝謝幫助壽Sooper。 – user244228 2011-12-25 23:36:01

+0

爲什麼呢?如果你有'如果(修剪($ _ POST ['number'] =='')){...}''''''''''''''''''''如上圖所示,只需在花括號內放置另一個'if'語句就可以搜索'嵌套if語句' – sooper 2011-12-25 23:41:10