使用ajax进行表单验证不会返回true

时间:2013-09-17 16:51:07

标签: jquery validation return-value

我正在进行表单验证以更改密码第一个字段模糊检查密码是否正确使用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;    
}
}

1 个答案:

答案 0 :(得分:0)

我还没有太多时间来审核javascript代码。你可以认为你曾经使用单引号进行写法,而不是在noty中传递给function confirmpass调用的文本属性(在哪里定义了该函数?)?

function confirmpass(){
var newpass1 = $("#new_pass1").val();
var newpass2 = $("#new_pass2").val();
if(newpass1 != newpass2){
            noty({
                layout: 'center',
                  theme: 'defaultTheme',
                  type: 'error',
                  text: 'passwords don't match',  <<----