-1

我必须在 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);
        }
    })

});
4

2 回答 2

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
            }
        }
    });
});
于 2013-03-03T20:57:43.737 回答
-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}                                         }
于 2013-03-14T10:29:25.570 回答