Problem with jQuery validate plugin (remote validation)

I have a problem trying to validate a user value using the jQuery Validation plugin.

The validation seems to fire correctly and call the web service function exactly as I want but, even if the server function does work correctly and returns a true/false result the field is always invalid.

This is the validation code on the client side

$('#myForm').validate({
    errorContainer: container,
    errorLabelContainer: $("ol", container),
    wrapper: 'li',
    meta: "validate",
    rules: {
        Code: { required: true, maxlength: 15, remote: function () {
            return {
                type: "POST",
                url: GetBaseWSUrl() + 'MyService.asmx/IsValidCode',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify({ umltCode: $('#Code').val() })
            }
        }
        },
        Description: { required: true, maxlength: 255 },
        Notes: { maxlength: 255 }
    },
    messages: {
        // ... omitted for brevity
    },
    submitHandler: function (form) {
        saveObject(form);
    }
});

Using fiddler I am able to see that a call is made to the server and that the server is returning a json true/false value depending on the case as in the following sample:

{"d":false}

or

{"d":true}

Despite this, the plugin still mark the field as invalid. Any suggestion?

EDIT: this is my server function

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyService : System.Web.Services.WebService
{
    [WebMethod]
    public object IsValidCode(string umltCode)
    {
        [...]

        if (u != null)
            return false;

        return true;
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The problem is that instead of

true

your web service returns

{"d":true}

The d property should be removed.

ASMX is considered legacy so I would get rid of it at the first place. But until then you could also use the dataFilter property, like this:

remote: function() {  
    return {  
        type: "POST",
        url: GetBaseWSUrl() + 'MyService.asmx/IsValidCode',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify({ umltCode: $('#Code').val() }),
        dataFilter: function (data) {
            var x = (JSON.parse(data)).d;
            return JSON.stringify(x); 
        }  
    };   
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x