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

Form Validation with PHP not working all if statements (debug help)

我有注册表单,但不是所有的 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>'; 

因此您正在设置 "characters" 字符串并与 "character" 字符串进行比较,如果比较不正确

如果你是 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;
  }

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

旁注:md5() 不是正确的加密方法。它只创建一个散列。