I\'m trying to figure out how I can turn this:
$(\'#username\').blur(function(){
$.post(\'register/isUsernameAvailable\',
{\"username\":$(\'#
The easiest way to accomplish this is to simply return true
, an error message as a string, or false
from your server-side resource. According to the jQuery validate documentation for remote:
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.
This means if you can change your server-side code to return true
in the event of successful validation or an error message ("username already in use") in the event of unsuccessful validation, you could just write the following remote rule:
remote: {
type: 'post',
url: 'register/isUsernameAvailable',
data: {
'username': function () { return $('#username').val(); }
},
dataType: 'json'
}
You could also simply return true
or false
from your server-side resource and define the error message on the client. In that case you would have the above rule and then a property in the messages
object:
messages: {
username: {
remote: "username already in use"
}
}
rules: {
username: {
minlength: 6,
maxlength: 12,
remote: 'register/isUsernameAvailable',
}
}
You need to pass username