使用 PHP 的表单验证不能在所有 if 语句中工作(调试帮助)



我有我的注册表,但不是所有的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 个字符的限制内,则注册没有问题。

如果发生错误,则放置此字符串:

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>'; 

所以你正在设置"字符"字符串并与"字符"字符串进行比较,如果比较不为真

如果你是 f9 与 jquery 或 js,那么你可以使用下面的代码实现

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

网页代码

 <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()"/>

旁注:md5(( 不是正确的加密。它只创建一个哈希。

最新更新