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

时间:2017-05-26 10:30:10

标签: php forms validation syntax-error registration

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

3 个答案:

答案 0 :(得分: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>'; 

所以你设置“characters”字符串并与“character”字符串比较,如果比较不正确

答案 1 :(得分:2)

如果您使用jquery或js是f9,那么您可以使用以下代码实现

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

答案 2 :(得分:1)

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