jQuery custom unobstrusive validator not working p

2019-08-03 17:08发布

问题:

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?