jQuery custom unobstrusive validator not working properly

戏子无情 提交于 2019-12-24 07:12:38

问题


I have written a custom jQuery unobstrusive validation rule which looks like this:

<script>
    $.validator.addMethod("emailvalidate", function (value, element)
    {
        {
            var isSuccess;
            $.post("/Front/CheckEmail", { email: value }).done(function (data) {

                console.log("called");
                isSuccess = data === "true" ? true : false
            });
            console.log(isSuccess);
            return isSuccess;
        }});
    $.validator.unobtrusive.adapters.add("emailvalidate", function (options) {
        options.rules["emailvalidate"] = true;
        if (options.message) {
            options.messages["emailvalidate"] = options.message;
        }
    });
</script>

Please note the part with:

Console.log("called");

and this one:

console.log(isSuccess);

The way this gets written out in console is like following:

"undefined"
"called"

Which means the variable is returned before I can even make the call towards myserver, thus leaving the field always invalidated...

what am I doing wrong here guys?

来源:https://stackoverflow.com/questions/44998226/jquery-custom-unobstrusive-validator-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!