I am having troubles getting a return string passed back to the validator.
Here is my js
$('#frmSignUp').validator({
custom: {
emailexist: function ($el) {
var result = checkUserEmail($el.val())
.done(function (r) {
if (!r.success) {
// email already in use
return r;
}
})
return result.responseText;
}
}
});
function checkUserEmail(name) {
return $.ajax({
url: '@Url.Action("CheckUserName","Account")',
data: { ChapterPOCEmail: name },
type: 'POST',
dataType: 'json'
});
}
in chrome dev tools the "r" does indeed have the full json returned from the controller
{"success":false,"responseText":"That email is already in use."}
However, the validator isn't receiving the string, and therefor is still showing the green check mark in the feedback, and no message in the help-block. I have tried to use the "data-remote", but I couldn't get asp.net to return a status code along with a message correctly. it was overriding any custom message I sent back , and the validator couldn't receive it.
Any help on my issue would be appreciated.
ajax
is an async method by deafult and does not return any value.
You can force it to be sync though or use promises, and use it's value on success for example...
change your checkUserEmail
with this:
function checkUserEmail(name) {
var r = false;
$.ajax({
url: '@Url.Action("CheckUserName","Account")',
data: { ChapterPOCEmail: name },
type: 'POST',
dataType: 'json',
async: false // wait for the call, dont treat as an async call
}).done(function(result) {
r = result === "OK";
}).fail(function (err) {
console.log(err, "something went awire");
}).always(function () {
console.log("all done");
});
return r;
}
I assume that your ajax call returns OK
, remember to change it accordingly.