开发者

jQuery with remote validation does not wait for answer from server

I've noticed, that sometimes my validation code works wrong:

var $validator = $("#checkoutForm").validate();

...

if (!$validator.element($sameShippingAddress)) {
     ...       
}

Debugging with Firebug showed, that sometimes $validator.element($sameShippingAddress) would return undefined (I guess it just does not wait till response is returned) and that wo开发者_开发问答uld be assumed as false, even if element is valid.

If add code like this before if statement, everything works fine:

 while (validator.element($sameShippingAddress) !== undefined) {

 }

Question is if that is right solution and there's no better way to handle problem with validation plugin itself?

Update: I'm using http://bassistance.de/jquery-plugins/jquery-plugin-validation/


Infinite while loop on validator variable is not a good choice. Instead use the code below that utilise Javascript timer. You can show animated processing/server response graphics after validate() method.

var validator = $('#resetpassword').validate({///your code...})

    doTimer();

    function timedCount()
    {
        t=setTimeout("timedCount()",1000);
    }

    function doTimer()
    {
    if (validator === undefined)
      {
        timedCount();
      }
    }

if(validator==true)
    $('#form').ajaxSubmit(options);


It's hard to tell how you are handling successful submissions or if you uses the css class to denoted required fields, but the following is how it's done in the demo for the plugin:

$.validator.setDefaults({
    // code to be executed on submit
submitHandler: function() { alert("submitted, replace this with your server request");}
});

$().ready(function() {
    // validate the comment form when it is submitted
        // use css class .required for fields you want the validator to check
        // if your form is valid then it is handled by the submitHandler
        // if not the plugin displays error messages
    $("#checkoutForm").validate();
    //validate can take a block for custom validation and error messages
});

Hope this helps you find a solution and isn't just more of the same (also just realized this question is a year old, but I already wrote this so...)


Obiously, it is not the best solution. Instead add

if (!$validator.element($sameShippingAddress)) { ...
}

in the Ajax callback function.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜