我有我的註冊表單,但並非所有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個字符的限制內放置了通行證,則註冊沒有問題。
剛剛測試了您的代碼。一切看起來都不錯 http://sandbox.onlinephpfunctions.com/code/9ae4124447b43479d39b74d648dfabc685682005 – Rulisp
所以'var_dump(strlen($ password))' –
觀察;沒有初始化$ error_array。並且在下面的語句後添加Php close標籤if(empty($ error_array)){$ password = md5($ password);或者你應該closeit或者你必須在php變量 – siva