jquery validation - addMethod/remote - error message in json response jquery validation - addMethod/remote - error message in json response json json

jquery validation - addMethod/remote - error message in json response


I ended up figuring it out. The return success is what I was failing - I kept returning, returning true, or returning false - and every one of them would "fail" validation.

remote: {    type: "POST",    url: "js/username.json",    contentType: "application/json; charset=utf-8",      dataType:"json",    data: "{'" + $('#enterEmail').attr('id') + "': '" + $('#enterEmail').val() + "'}",    dataFilter: function(data) {        var json = JSON.parse(data);        if(json.isError == "true") {            return "\"" + json.errorMessage + "\"";        } else {            return success;        }    }}


Have a look at validval.

I found it fairly easy to integrate, tweak (custom validations, custom alerts), plus fiddle it into AJAX like here

EDIT
So where are you posting your data object to? What does js/username.json do? If you post there you have to do something there to return the object in your desired format.

So if you want to check for unique email, I'm posting to the server, do a check there, construct an object

 object.response.success = true/false object.response.data = "some text to display"

and then return the object.response via AJAX.

Like so:

 $.ajax({      async: false,      type: "post",      url: "somewhere_to_handle_username_check",          data: data: "{'" + $('#enterEmail').attr('id') + "': '" + $('#enterEmail').val() + "'}",      contentType: "application/json; charset=utf-8",      dataType: "json",      // AJAX OK, return true/false      success: function(objResponse){           if (objResponse = "true"){                alert("success");            } else {                alert("failed, username already taken");            }      // AJAX Failed      error: function () {         alert("failed - AJAX failed");        }    });

I'm usually validating client side, that an email address was entered (using above validaval) and then send the email adress to the server for checking the value and constructing the response object.

Hope this helps. If not, let me know what your AJAX request should do and what js/username.json does.