php
  • regex
  • forms
  • validation
  • 2015-11-19 100 views -2 likes 
    -2

    我有這段代碼,我不知道爲什麼它不會驗證。當它發現錯誤時,應顯示每個包含無效信息的無效字段,但不知何故,它顯示所有字段無效。這裏是我的代碼:驗證正則表達式在php

    <?php 
    $debug =1; 
    
    $fname =""; 
    $valid_fname = 0; 
    $fname_regex = '/^([A-Z][a-z]){2,15}$/'; 
    $fname_error = '<span>First name must be 2-15 charachters long.</span>'; 
    
    
    $lname = ""; 
    $valid_lname = 0; 
    $lname_regex = '/^([A-Z][a-z]){2,15}$/'; 
    $lname_error = '<span>Last name must be 2-15 charachters long.</span>'; 
    
    $phone_number = ""; 
    $valid_phone = 0; 
    $phone_regex = '/^\d{3}-\d{3}-\d{4}$/'; 
    $phone_error = '<span>Phone number must be xxx-xxx-xxxx format.</span>'; 
    
    $city = ""; 
    $valid_city = 0; 
    $city_regex = '/^([A-Z][a-z]){3,20}$/'; 
    $city_error = '<span>City name must be 3-20 charachters.</span> '; 
    
    $state = ""; 
    $valid_state = 0; 
    $state_regex = '/^([A-Z][a-z]){2}$/'; 
    $state_error = '<span>State name must be 2 charachters.</span>'; 
    
    $error_text = ""; 
    $output_form = 1; 
    
    if(isset($_POST['submit'])) { 
    
        /*if($debug) { 
         echo "<pre>"; 
         print_r($_POST); 
         echo "</pre>"; 
        }*/ 
        //debug only 
    
        $fname = trim($_POST['fname']); 
        $lname = trim($_POST['lname']); 
        $phone_number = trim($_POST['pnumber']); 
        $city = trim($_POST['city']); 
        $state = trim($_POST['state']); 
    
        if (preg_match($fname_regex, $fname)) { 
         $valid_fname =1; 
        } else { 
         $error_text.= "$fname_error<br>\n\r"; 
        } 
    
        if (preg_match($lname_regex, $lname)) { 
         $valid_lname =1; 
        } else { 
         $error_text.= "$lname_error<br>\n\r"; 
        } 
    
        if (preg_match($phone_regex, $phone_number)) { 
         $valid_phone =1; 
    
        } else { 
         $error_text.= "$phone_error<br>\n\r"; 
        } 
    
        if (preg_match($city_regex, $city)) { 
         $valid_city =1; 
        } else { 
         $error_text.= "$city_error<br>\n\r"; 
        } 
    
        if (preg_match($state_regex, $state)) { 
         $valid_state =1; 
        } else { 
         $error_text.= "$state_error<br>\n\r"; 
        } 
    
    if ($valid_fname && $valid_lname && $valid_phone && $valid_city && $valid_state) { 
         $output_form = 0; 
    
        } else { 
         $error_text .= "<span>Please complete all the fields.</span>"; 
        } 
    
    
    }//end of if(isset($_POST['submit'])) 
    
    require_once("./inc/head.inc.php"); 
    

    >

    <body> 
        <div id="wrapper"> 
        <?php require_once("./inc/header.inc.php"); ?> 
         <div class="content"> 
         <?php 
          if($output_form) { 
         ?> 
          <h3>Please fill out following information.</h3> 
          <?= $error_text ?> 
          <form class="myform" action="<?= $_SERVER['PHP_SELF'] ?>" method="post"> 
           <p>First Name: <input name="fname" type="text" value="<?= $fname ?>"> 
           <p>Last Name: <input name="lname" type="text" value="<?= $lname ?>"> 
           <p>Phone Number <small>(Must be in xxx-xxx-xxxx format)</small><input name="pnumber" type="text" value="<?= $phone_number ?>"></p> 
           <p>City:<input name="city" type="text" value="<?= $city ?>"></p> 
         <p>State:<input name="state" type="text" value="<?= $state ?>"></p> 
           <div class="submit"> 
            <input name="submit" type="submit" value="Send"> 
           </div> 
          </form> 
         <?php 
          } else { 
         ?> 
    
         <div class="output"> 
         <h2>Your information is valid:</h2> 
         <p>Hello! <?= $fname .' '.$lname?></p> 
         <p>Phone: <?= $phone_number ?></p> 
         <p>Ciy, State : <?= $city . '' . $state ?></p> 
         </div> 
         </div> 
         <?php 
         } 
         ?> 
        </div> 
    
        <?php require_once("./inc/footer.inc.php"); ?> 
    </body> 
    

    回答

    1

    你的正則表達式不正確。它將搜索像AaAsBcDe字符串(一對大寫和小寫字符,和2-15事件是對的)

    使用以下命令:

    /^[A-Za-z]{2,15}$/ 
    

    說明:

    • ^:字符串開頭
    • [A-Za-z]:任何大寫或小寫字母
    • {2-15}:前一個字符的2至15 occurances設置
    • $:字符串

    同樣可以修改別人的結束。

    1

    更改托架位置: 示例:

    $fname_regex = '/^([A-Z][a-z]){2,15}$/'; 
    

    $fname_regex = '/^([A-Z][a-z]{2,15})$/'; 
    
    相關問題