2017-05-26 46 views
-1

我有我的註冊表單,但並非所有if語句正在工作。請問在哪裏是我找不到的錯誤使用PHP進行表單驗證並非所有if語句都能正常工作(調試幫助)

$password = ""; //Password 
$password2 = ""; //COnfirm Password 

//Pass 
$password = strip_tags($_POST['reg_password']); //Remove html tags 
$password2 = strip_tags($_POST['reg_password2']); //Remove html tags 

//This if is working and return error 
if($password != $password2) { 
    array_push($error_array, 'Your passwords do not match'); 
} 
else{ 
    //This one is working as well 
    if(preg_match('/[^A-Za-z0-9]/', $password)){ 
     array_push($error_array, 'Special characters like "£%^*() are not allowed'); 
    } 
} 

// I have the problem with this one. If user put 3 chars i don't get the error messages 
// The form is not submitting but the error messages are not displayed 
if(strlen($password) > 20 || strlen($password) < 5){ 
    array_push($error_array, 'Your password must be between 5 and 20 characters'); 
} 

if(empty($error_array)){ 
    $password = md5($password); //Encrypt pass before send to db 

    <div class="form-group"> 
    <label class="sr-only" for="reg_password2">Confirm Password</label> 
     <input type="password" name="reg_password2" placeholder="Password..." class="form-password form-control" id="reg_password2"> 
      <span id="errorPassword2" class="text-danger"></span> 
      <?php if(in_array('Your passwords do not match', $error_array)) 
        echo '<span id="errorPass" class="text-danger">Your passwords do not match</span>'; 
       else if(in_array('Special characters like "£%^*() are not allowed', $error_array)) 
        echo '<span id="errorPass" class="text-danger">Special characters like "£%^*() are not allowed</span>'; 
       else if(in_array('Your password must be between 5 and 20 character', $error_array)) 
        echo '<span id="errorPass" class="text-danger">Your password must be between 5 and 20 character</span>'; 
    ?>        
    </div> 

除了特定的if選項,一切都正常工作。但是,如果用戶在5-20個字符的限制內放置了通行證,則註冊沒有問題。

+0

剛剛測試了您的代碼。一切看起來都不錯 http://sandbox.onlinephpfunctions.com/code/9ae4124447b43479d39b74d648dfabc685682005 – Rulisp

+0

所以'var_dump(strlen($ password))' –

+0

觀察;沒有初始化$ error_array。並且在下面的語句後添加Php close標籤if(empty($ error_array)){$ password = md5($ password);或者你應該closeit或者你必須在php變量 – siva

回答

2

你把這個字符串,如果發生錯誤:

if(strlen($password) > 20 || strlen($password) < 5){ 
    array_push($error_array, 'Your password must be between 5 and 20 characters'); 
} 

但是,當你想顯示錯誤消息,您是在比較字符串:

else if(in_array('Your password must be between 5 and 20 character', $error_array)) 
       echo '<span id="errorEmail" class="text-danger">Your password must be between 5 and 20 character</span>'; 

所以要設置「字符」字符串,與「字」的字符串對比,如果如果妳使用jQuery或JS然後F9 u能實現使用下面的代碼比較不爲真

+0

謝謝...我一直在尋找像30分鐘,並認爲「什麼是錯的一切都好」..大聲笑謝謝你 – Maria

+0

np,這些事情發生:) – Gerard

2

   function Validate() 
    { 
    var password = document.getElementById("txtPassword").value; 
    var confirmPassword = document.getElementById("txtConfirmPassword").value; 
    if (password != confirmPassword) { 
     alert("Passwords do not match."); 
     return false; 
    } 
    return true; 
    } 

HTML代碼

<input type="password" name="passwd" id="txtPassword" placeholder="Password" required="required"> 
    <input type="password" name="rpasswd" id="txtConfirmPassword" placeholder="repeatPassword" required="required" onChange="return Validate()"/> 
1

邊注:MD5()不是一個適當的加密。它只創建一個散列。