2017-05-26 6 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クローズタグは、次の文の後にmisedされます。if(empty($ error_array)){$ password = md5($ password);いずれかの閉鎖する必要がありますか、PHP変数の1つであなたのマークアップをconcatinateする必要があります – 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>'; 

ですから、「文字」の文字列を設定していると、比較が真でない場合は "文字"文字列と比較します。

+0

ありがとうございました...私は30分のように探して、 "何が間違っているのはすべて大丈夫"と思っていました.. Lolありがとう – Maria

+0

np、これらのことが起こります:) – Gerard

2

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

サイドノート:MD5()は、適切な暗号化ではありません。ハッシュのみを作成します。

関連する問題