I am having difficulty trying to get my field to validate using the remote rule with jQuery Validation. It is making the ajax call fine and passing the correct data. The Web service is operating fine and is returning a true or false as needed.
My problem comes in that the rule always stays negative. I am not sure if maybe I am returning the true/false in the incorrect format. Ideas/thoughts?
This is my rule:
$("[id$=txtOther]").rules("add", {
remote: function () {
return {
type: "POST",
url: $("[id$=hBaseURL]").val() + "Webservice/Validation.asmx/ValidateUser",
data: JSON.stringify({ FullName: $("[id$=txtOther]").val(), UserID: $("[id$=txtOtherID]").val() }),
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
};
},
messages: {
remote: ""
}
});
And this is how my web service is responding:
d [Object {__type="Validation+Results", Status="true"}]
0 Object {__type="Validation+Results", Status="true"}
Status "true"
__type "Validation+Results"
Well, this is what I ended up having to do to fix the problem. First I took what Andrew said and found more out there to support what he was saying. The most helpful was this post:
Problem with jQuery validate plugin (remote validation)
This post basically added these lines to my existing code:
The problem was still there though. I started thinking about what could be wrong and started to wonder about my web service and how it was returning other than the .d response. I had used different modifications of this web service for some time now, but for some reason this one was not working. After looking a bit more at it I thought about the fact that it was returning a list of values since that is what I needed everywhere else. I was only returning one item in that list, but I was wondering what would happen if I returned just that item instead of the list. My web service now returned this:
This still did not work with my original code, but now I was fairly certain it was because it was returning as d. The dataFilter I found at the other site was still not allowing it to work so I modified the dataFilter a little bit to return just plain text like this:
Finally I had a solution that was working. During this process I ran into issues with getting the validation to trigger again after it failed, or supposedly returned true even though it was in error. There is a lot more information out there about this and since that wasn’t the original question I won’t go into all of that. One helpful post was:
jquery validation – remote method won't trigger after valid However, just because they are so related I used this code to reset the validation allowing the validation to be triggered again even with a recent true valid call.
I am still using this code along with the following code to trigger the validation on demand:
This is my final code for the remote validation: