jQuery Validate插件远程规则不返回boolean

时间:2013-03-03 20:13:41

标签: jquery ajax jquery-validate jsonp

我必须在jQuery验证器中调用带有远程字段的servlet来检查是否已存在插入的用户名。问题是我必须在JSONP中调用它并且它既不返回布尔值也不返回字符串,而是返回带有可能替代的Json。有什么帮助吗?

使用此方法,它始终表示用户已存在!

    function parseUser(a){return a}

    $.validator.addMethod("checkUserUnique", function(value, element) {
    $.ajax({
        url:'myservletJSONP.do',
        data: {'username':function(){return $("#registration-form1 :input[name='username']").val()}},
        dataType: "jsonp",
        jsonp: false,
        jsonpCallback: 'parseUser',
        success: function(data){
            if(data[0].result=='exist'){
                return false;
            }else{
                return true;
            }
        },
        error: function(er){
            console.log("error: "+er);
        }
    })

});

2 个答案:

答案 0 :(得分:0)

是的,remote规则正在寻找布尔值。

如果你无法做到这一点,那么另一种方法是创造&请改用自定义规则。在您的自定义方法中,您可以编写所需的任何功能...运行ajax,测试结果,只需return true即可传递经过验证的字段。

http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage

jQuery.validator.addMethod("myRule", function(value, element) { 
    // your ajax
    // if ( your ajax result == "something" ) {
        // return false; // represents failed validation
    //} else {
        // return true; // represents passed validation
    //}
}, "failed - your custom message");

回调的参数是:

  • 已验证元素的当前
  • 要验证的元素


$(document).ready(function() {
    $('#myform').validate({
        rules: {
            myfield: {
                myRule: true  // instead of remote
            }
        }
    });
});

答案 1 :(得分:-1)

这就是答案:

    $.validator.addMethod("remoteValidator", function(value, element, param) {
        var previous = this.previousValue(element);
        if (!this.settings.messages[element.name] ) {
            this.settings.messages[element.name] = {};
        }
        previous.originalMessage = this.settings.messages[element.name].remote;
        this.settings.messages[element.name].remote = previous.message;
        param = typeof param === "string" && {url:param} || param;
        if ( previous.old === value ) {
            return previous.valid;
        }
        previous.old = value;
        var validator = this;
        this.startRequest(element);
        var data = {};
        data[element.name] = value;
        $.ajax($.extend(true, {
            url: param.url,
            data: function(){return param.data || data}(),
            dataType: "jsonp",
            jsonp: false,
            jsonpCallback: param.callback,
            success: function( response ) {
                var valid = param.checkValidity(response) 
                if ( valid ) {
                    var submitted = validator.formSubmitted;
                    validator.prepareElement(element);
                    validator.formSubmitted = submitted;
                    validator.successList.push(element);
                    delete validator.invalid[element.name];
                    validator.showErrors();
                } else {
                    var errors = {};
                    var message = validator.settings.messages[element.name]["remoteValidator"];
                    errors[element.name] = message;
                    validator.invalid[element.name] = true;
                    validator.showErrors(errors);                       
                }
                previous.valid = valid;
                validator.stopRequest(element, valid);
            }
        }, param));
        return "pending";
});

你可以称它为你的规则添加这些行:

remoteValidator: {'url':'myServlet','checkValidity': function(resp){if(someStuff)return true; return false}                                         }