0

I'm rewriting some javascript validation using jQuery Validate plugin

One problem that has occurred is with a function that checks if the username is valid or not. It does this by sending an ajax call containing the username.

The request URL needs to look like this:

/ajax/check-username/usernamehere

Current function:

var checkUsername = function(value){
    var username = false;
    jQuery.ajax({
        url: "/ajax/check-username/" + encodeURI(value)
    }).done(function (data) {
        if (data.exists) {
            username = false;
        } else {
            username = true;
        }
    });
    return username;
}

jQuery Validate request path looks like this:

/ajax/check-username/?username=usernamehere

jQuery Validation:

jQuery('#user').validate({
    rules: {
        username: {
            required: true,
            minlength: 6,
            remote: {
                url: '/ajax/check-username/'
            }
        }
    }
});

How can I get the remote method to use my current url routing?

1
  • I edited your tags. The jquery-validation-engine is a totally different plugin. Commented Sep 10, 2013 at 15:19

1 Answer 1

0

Your original JavaScript function is looking to see if data exists.

if (data.exists) {
    username = false;
} else {
    username = true;
}

However, as per documentation for the remote rule, you'll need to return true from the server script in order to pass validation. Almost any other response will trigger a validation error message.

"The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. 'That name is already taken, try peter123 instead' to display as the error message."

Your best solution would be to modify your server script at /ajax/check-username/ so that it returns true when no data exists; and either false or your desired error message otherwise.


Quote OP:

"How can I get the remote method to use my current url routing?"

Short answer: you don't.

The remote method is sending the data to your server script as JSON dataType using the following default settings...

dataType: "json",
data: { nameOfTheElement: valueOfTheElement }

So you could modify the server script to simply accept the incoming data in this JSON format.

Otherwise, you could change the remote settings when you declare it.

An example taken from the docs:

rules: {
    username: {
        required: true,
        minlength: 6,
        remote: {
            url: "/ajax/check-username/",
            type: "post",
            data: {
                username: function() {
                    return $( "#username" ).val();
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Changing the response is not the issue. The issue is, that the URLs are not correct. /ajax/check-username/?username=testusername needs to look like: /ajax/check-username/testusername.
@CharliePrynn, please read the second half of my answer. And from the limited code you posted in your question, it looked to me like the response would also be a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.