Check if passwords match jQuery
I am trying to test two inputs to match each other using jquery and I couldn't find a way to do this. I can't get jquery to get the values ββof the two inputs, so it can't check if they don't match. input is done using a code, so for those who are not familiar with it - the identifier of the first input is "pass", and the second is "pass2"
<tr>
<td>choose a password:</td>
<td><?= form_password('password', '', 'placeholder="choose a password" required="required" id="pass"') ?></td>
</tr>
<tr>
<td>re-type the password:</td>
<td><?= form_password('password2', '', 'placeholder="re-type your password" required="required" id="pass2"') ?></td>
</tr>
script -
$(document).ready(function(){
var pass = $('#pass').val();
var pass2 = $('#pass2').val();
$('#pass2').focusout(function(){
if(pass != pass2){
alert('the passwords didn\'t match!');
}
});
});
+3
source to share
2 answers
because you are reading the values ββin the finished document, not when the user changed them. Move it inside focus.
$(document).ready(function(){
$('#pass2').focusout(function(){
var pass = $('#pass').val();
var pass2 = $('#pass2').val();
if(pass != pass2){
alert('the passwords didn\'t match!');
}
});
});
+10
source to share