我想验证两个输入的密码是否相同。但我似乎无法得到它的工作。无论我在输入字段中输入什么值,结果总是“真”。你能看到我做错了什么吗?检查密码是否相等jQuery
HTML:
<div class="form-group" id="password">
<input type="password" class="form-control" placeholder="Password" name="password">
</div>
<div class="form-group" id="repassword">
<input type="password" class="form-control" placeholder="Confirm Password" name="repassword">
</div>
的jQuery:
//Check if password is set
$('input[name=password]').blur(function() {
if($(this).val().length == 0){
$('#password').addClass('has-error');
} else {
$('#password').addClass('has-success');
}
});
//Check if repassword is set
$('input[name=repassword]').blur(function() {
if($(this).val().length == 0){
$('#repassword').addClass('has-error');
} else {
$('#repassword').addClass('has-success');
}
});
//Check if password and repassword are equal
$('input[name=password]').blur(function() {
if ($(this).attr('value') !== $('input[name=repassword]').attr('value')) {
$('#password').addClass('has-error');
$('#repassword').addClass('has-error');
} else {
$('#password').addClass('has-success');
$('#repassword').addClass('has-success');
}
});
是否有理由将这些操作分开绑定? – circusdei
你正在使用'val()',所以你知道它存在,但出于某种原因,你突然决定使用'attr('value')',为什么? – adeneo
请告诉我你也有服务器代码,也检查。对密码客户端做任何事情都是可怕的。 – asawyer