我有一个表单验证来更改密码第一个字段模糊检查密码是否正确使用 ajax 和其他两个字段输入新密码并检查它们是否匹配但它根本不返回 true 表单不提交虽然密码正确且其他密码匹配,但我不知道错误在哪里,这是代码 html:
<form id="pwd" method="post" action="" onsubmit="return validate();">
<table width="400" border="1">
<tr>
<td>current password</td>
<td><input type="password" class="user_text" name="old_pass" id="old_pass" onblur="check_oldpass();" /></td>
</tr>
<tr>
<td>new password</td>
<td><input type="password" class="user_text" name="new_pass1" id="new_pass1" /></td>
</tr>
<tr>
<td>confirm password</td>
<td><input type="password" class="user_text" name="new_pass2" id="new_pass2" onblur="confirmpass();" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="update_pass" value="حفظ" id="update_pass" /></td>
</tr>
</table>
</form>
jQuery代码:
function check_oldpass(){
var oldpass = $("#old_pass").val();
var ajax = false;
ajax = new XMLHttpRequest();
ajax.open("POST","checkpass.php?pass="+oldpass);
ajax.onreadystatechange = function(){
if(ajax.readyState == 4 && ajax.status == 200){
var passresponse = ajax.responseText;
if(passresponse.indexOf('wrong') !== -1){
noty({
layout: 'center',
theme: 'defaultTheme',
type: 'error',
text: 'password is not correct',
dismissQueue: true, // If you want to use queue feature set this true
template: '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
animation: {
open: {height: 'toggle'},
close: {height: 'toggle'},
easing: 'swing',
speed: 500
},
timeout: 2000,
force: false,
modal: true,
closeWith: ['click'],
callback: {
onShow: function() {},
afterShow: function() {},
onClose: function() {},
afterClose: function() {}
}
});
return false;
}
else{
return true;
}
}
}
ajax.send(null);
}
function confirmpass(){
var newpass1 = $("#new_pass1").val();
var newpass2 = $("#new_pass2").val();
if(newpass1 != newpass2){
noty({
layout: 'center',
theme: 'defaultTheme',
type: 'error',
text: 'passwords do not match',
dismissQueue: true, // If you want to use queue feature set this true
template: '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
animation: {
open: {height: 'toggle'},
close: {height: 'toggle'},
easing: 'swing',
speed: 500
},
timeout: 2000,
force: false,
modal: true,
closeWith: ['click'],
callback: {
onShow: function() {},
afterShow: function() {},
onClose: function() {},
afterClose: function() {}
}
});
return false;
}
else{
return true;
}
}
function validate() {
$.each($("form#pwd :input"),function(){
$(this).blur();
});
if(!check_oldpass() || !confirmpass){
return false;
}
else{
return true;
}
}