【问题标题】:form validation using ajax doesn't return true使用 ajax 的表单验证不返回 true
【发布时间】:2013-09-22 05:24:43
【问题描述】:

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

【问题讨论】:

  • 天哪!一个可行的例子怎么样,没有多少人会解决这个问题!

标签: jquery validation return-value


【解决方案1】:

我没有花太多时间来查看 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',  <<----

【讨论】:

  • 我已经删除了撇号,但仍然是同样的问题
  • 您正在使用 POST 方法进行 ajax 调用,并在 URL 中传递参数 pass。这不是正确的方法。
  • 您在脚本中使用了 jQuery。我可以向您推荐使用 jQuery 中的 ajax 工具。作为使用示例:link
猜你喜欢
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-19
  • 2013-01-10
相关资源
最近更新 更多