jQuery validate plugin:有效的远程调用bypassess其他无效检查

时间:2012-12-13 17:54:35

标签: jquery jquery-validate

我有一个远程规则和一些其他基本必需规则。奇怪的是,如果我的远程规则通过,但其他一些规则失败,它将继续并提交表单。我已经尝试将远程规则移动到列表的后面或前面,看它是否有所不同,但事实并非如此。有没有人遇到过这个?

http://jsfiddle.net/LU5sU/7/(要通过验证,请在促销代码框中输入test。)

$('#ticket-form').validate({
onfocusout: false,
onkeyup: false,
onclick: false,
rules: {
    code: {
        required: true,
        remote: {
            url: "http://www.bobmurdoch.com/ajax.php",
            type: "post",
            async: false,
            data: {
                action: "check_code",
                code: function () {
                    return $("#code").val();
                }                    
            }
        }
    },
    first_name: {
        required: true
    },
    last_name: {
        required: true
    },
    company: {
        required: true
    },
    address: {
        required: true
    },
    city: {
        required: true
    },
    state: {
        required: true
    },
    zip: {
        required: true
    },
    phone: {
        required: true
    },
    email: {
        required: true,
        email: true
    },
    survey_question_1: {
        required: false
    }
},
messages: {
    code: {
        required: "Please enter a valid promo code",
        remote: "Please enter a valid promo code"
    },
    first_name: "Please enter a value for First Name",
    last_name: "Please enter a value for Last Name",
    company: "Please enter a value for Company",
    address: "Please enter a value for Address",
    city: "Please enter a value for City",
    state: "Please enter a value for State",
    zip: "Please enter a value for Zip/Postal Code",
    phone: "Please enter a value for Phone",
    email: {
        required: "Please enter a valid e-mail address",
        email: "Please enter a valid e-mail address"
    }
},
showErrors: function (errorMap, errorList) {
    var errors = '';
    for (var i in errorList) {
        errors = errors + errorList[i].message + "\n"
    }
    if (errors != '') {
        alert(errors);
    }
}
});

1 个答案:

答案 0 :(得分:0)

关注the online documentation for remote:后,我从async: false,移除了remote:,它似乎正在按预期运行。

根据文件:

$("#myform").validate({
  rules: {
    email: {
      required: true,
      email: true,
      remote: {
        url: "check-email.php",
        type: "post",
        data: {
          username: function() {
            return $("#username").val();
          }
        }
      }
    }
  }
});

jsFiddle Demo

您修改过的代码:

    code: {
        required: true,
        remote: {
            url: "http://www.bobmurdoch.com/ajax.php",
            type: "post",
            data: {
                action: "check_code",
                code: function () {
                    return $("#code").val();
                }                    
            }
        }
    },

根据OP的评论进行编辑:

通过删除自定义函数以在弹出窗口中放置错误消息并使用默认的errorPlacement:函数,我可以证明这是有效的。输入在无效时显示.invalid类,在有效时显示.valid类。您的问题在其他地方,与我的解决方案无关。

请参阅演示:http://jsfiddle.net/LU5sU/11/