开发者

jQuery Validator -- custom method does not hide error when valid

I have a custom validator checking a value against a database of acceptable values via an ajax request. I thought at first it wasn't hiding the error message because the request was being done asynchronously, so then it could perhaps fail to return true or false in time for the validator to recognize this.

I turned async off and the error message still remains shown. How can I get this to go away?

Through console logging it does return true, yet the error message still remains. I tried to explicitly hide the message, but the validator still assumes false and fails to submit.

jQuery.validator.addMethod("validZip", function(value, element){
    var _id = $(element).attr("id");
    $.ajax({
开发者_JAVA百科        url: '/xpress/wp-admin/admin-ajax.php',
        data: {action:"test_checkZip", zip:value},
        type:"POST",
        asyc: false,
        success: function(response){
            if (response== 0){
                return false;
            } else {
                //$("label[for="+_id+"][class=error]").hide();
                return true;
            }
        } 
    });
}, '*');


As Nicola pointed out, there's a typo in your callback. Apart from that it's the callback function that is returning true/false, so actually your validZip function is still returning 'undefined'.

Try this:

jQuery.validator.addMethod("validZip", function(value, element){
    var _id = $(element).attr("id");
    var isValid = false;
    $.ajax({
        url: '/xpress/wp-admin/admin-ajax.php',
        data: {action:"test_checkZip", zip:value},
        type:"POST",
        asyc: false,
        success: function(response){
            isValid = response!= 0;
        } 
    });
    return isValid;
}, '*');


Well i think you are checking against result shouldn't you check against response?

    success: function(response){
        if (response == false){
            return false;
        } else {
            //$("label[for="+_id+"][class=error]").hide();
            return true;
        }
    } 


Could it be because you have:

asyc: false,

as opposed to:

async: false,


You used an incorrect answer's code from your previous question, See my answer in that question to see what's wrong. Anyway you should do like this:

jQuery.validator.addMethod("validZip", function(value, element){
    var _id = $(element).attr("id");
    var return_value;
    $.ajax({
        url: '/xpress/wp-admin/admin-ajax.php',
        data: {action:"test_checkZip", zip:value},
        type:"POST",
        async: false,
        success: function(response){
            if (result == 0){
                return_value = false;
            } else {
                //$("label[for="+_id+"][class=error]").hide();
                return_value = true;
            }
        }
        return return_value; //HERE you have to return
    });
}, '*');

The problem is that you were returning you values inside a nested function, not your validator function.

Hope this helps. Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜