2016-08-16 194 views
3

請幫助,如何驗證此多個密碼並確認密碼是否相等?如果第一個judge_password []和juge_conPassword []不相等,則返回false,然後再次迭代到下一個。Javascript:驗證多個密碼並確認密碼字段

<input type="password" name="judge_password[]" class="judge_password"> 
<input type="password" name="judge_conPassword[]" class="judge_conPassword"> 

<input type="password" name="judge_password[]" class="judge_password"> 
<input type="password" name="judge_conPassword[]" class="judge_conPassword"> 

<input type="password" name="judge_password[]" class="judge_password"> 
<input type="password" name="judge_conPassword[]" class="judge_conPassword"> 
<input type="button" id="click"> 

我的代碼

var judge_conPassword = []; 

$('.judge_conPassword').each(function(){ 
    var theVal = $(this).val(); 
    if($.trim($(this).val())==''){ 
     pralse = false; 
     $('.trigger_danger_alert_changable').show().delay(5000).fadeOut(); 
     $('#palitan_ng_text').html('Confirm password required'); 
     return false; 
    } 
    judge_conPassword.push(theVal); 
}); 



var judge_password = []; 

$('.judge_password').each(function(){ 
    var theVal = $(this).val(); 
    if($.trim($(this).val())==''){ 
     pralse = false; 
     $('.trigger_danger_alert_changable').show().delay(5000).fadeOut(); 
     $('#palitan_ng_text').html('Password cannot be empty'); 
     return false; 
    } 
    judge_password.push(theVal); 
}); 

if(!pralse){ 
    return false; 
} 

function checkPass(judge_password, judge_conPassword){ 
    if(judge_password.length !== judge_conPassword.length){ 
     alert('password missing') 
    }else{ 
     var j_count = judge_password.length; 
     for(var i=0; i<j_count; i++){ 
      if(judge_password[i] !== judge_conPassword[i]){ 
       alert('Password not same'); 
       return false; 
      } 
     } 
       return true; 
    } 

} 
checkPass(); 
+0

您可以將這些字段的值存儲在兩個單獨的陣列和比較這些值 –

+0

有什麼問題了嗎?你的代碼看起來很好。 –

+0

@ HiI'mFrogatto他缺少.value,而比較 –

回答

0

你是不是比較值 試試這個

if(judge_password[i].value !== judge_conPassword[i].value) 
2

編輯:請注意,我之前我看到添加JS編輯張貼了這​​個答案到了這個問題,所以我剛剛關閉OP的HTML。

這是第一個想到的方法。我添加了一個mismatch類強調的是有問題的領域:

$("#click").click(function() { 
 
    var passwords = $("input.judge_password").removeClass("mismatch"), 
 
     confirms = $("input.judge_conPassword").removeClass("mismatch"), 
 
     valid = true; 
 
    
 
    passwords.each(function(i) { 
 
    if (this.value != confirms[i].value) { 
 
     valid = false; 
 
     $(this).add(confirms[i]).addClass("mismatch"); 
 
    } 
 
    }); 
 
    
 
    if (!valid) { 
 
    alert("Passwords don't match."); 
 
    } else { 
 
    // submit or whatever here 
 
    alert("Passwords match."); 
 
    } 
 
});
.mismatch { 
 
    background-color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="password" name="judge_password[]" class="judge_password"> 
 
<input type="password" name="judge_conPassword[]" class="judge_conPassword"> 
 
<br><br> 
 
<input type="password" name="judge_password[]" class="judge_password"> 
 
<input type="password" name="judge_conPassword[]" class="judge_conPassword"> 
 
<br><br> 
 
<input type="password" name="judge_password[]" class="judge_password"> 
 
<input type="password" name="judge_conPassword[]" class="judge_conPassword"> 
 
<br><br> 
 
<input type="button" id="click" value="Test">