Javascript: Validate multiple password and confirm password field

Please help, how do i validate this multiple password and confirm password if its not equal? If the first judge_password[] and juge_conPassword[] is not equal return false then iterate again to the next.

<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">

my code

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();

Upvotes: 3

Views: 782

Answers (2)

nnnnnn
nnnnnn

Reputation: 150070

EDIT: Note that I posted this answer before I saw the edit that added JS to the question, so I was going just off the OP's HTML.

Here's the first way that came to mind. I've added a mismatch class to highlight the fields that have a problem:

$("#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">

Upvotes: 2

Rashid Javed
Rashid Javed

Reputation: 62

You are not comparing the values try this

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

Upvotes: 0

Related Questions