Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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({
        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;
            }
        } 
    });
}, '*');
like image 754
Atticus Avatar asked Feb 04 '26 14:02

Atticus


1 Answers

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;
}, '*');
like image 153
mamoo Avatar answered Feb 06 '26 02:02

mamoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!