异步验证

时间:2014-01-30 13:52:24

标签: jquery validation asynchronous

我在我的项目中使用:http://www.jquery-steps.com/。一个很好的Jquery插件,用于添加向导。

如果服务器上有电子邮件,我想进行服务器验证。如果它无效,向导必须保持同样的步骤。如果有效,向导应转到下一步并获取与电子邮件相关的信息。

我试过

        onStepChanging: function (event, currentIndex, newIndex) {

            event.preventDefault();
            var action = ...
            ...

            if (currentIndex == 1) {
                var form = null;
                if (action === "import") {
                    form = $("#link-account-form")
                }
                else if (action === "create") {
                    form = $("#create-profile-form");
                }


                form.validate().settings.ignore = ":disabled,:hidden";
                var formValid = form.valid();
                if (formValid && newIndex == 2) {
                    if (action === "import") {
                        var login = form.find(".temp-login").val();

                        // here I want to get async validation to work...
                        return $app.getUserByLogin(login, function (userAccount, status, xhr) {
                             if (userAccount != null) {
                                $("#thirdStep .temp-name").val(userAccount.user.name);
                                $("#thirdStep .temp-gender").val(userAccount.user.gender);
                                var birthday = UI.Formatter.parseDatetime(userAccount.user.birthday);
                                $("#thirdStep .temp-birthdate").data('datetimepicker').setLocalDate(birthday);
                                $("#thirdStep .temp-telephone").val(userAccount.user.phone);
                                $("#thirdStep .temp-cellphone").val(userAccount.user.cellPhone);
                                $("#thirdStep .temp-address").val(userAccount.user.address);
                                $("#thirdStep .temp-neighborhood").val(userAccount.user.neighborhood);
                                $("#thirdStep .temp-zipcode").val(userAccount.user.zipCode);
                                $("#thirdStep .temp-city").val(userAccount.user.city);
                                $("#thirdStep .temp-state").val(userAccount.user.state);
                                $("#thirdStep .temp-id").val(userAccount.user.id);
                                $("#thirdStep .temp-picture-url").val(userAccount.user.pictureUrl);
                                return true;
                            } else {
                                UI.Notify.error("", $("#res").data("retrieve-error"));
                                return false;
                            }
                        }).fail(function (data, status, xhr) {
                            UI.Notify.error("", $("#res").data("retrieve-error"));
                            return false;
                        });
                    }
                    else if (action === "create") {
                        ...
                        return true;
                    }
                }
                return formValid;
            }
            return true;
        }

你是否已经面临这种情况?你是怎么做到的?

1 个答案:

答案 0 :(得分:0)

我在$ app.getUserByLogin中的ajax请求上使用async:false解决了这个问题。谢谢你的关注。