I am trying to create a custom method calling AJAX but it seems it is not working. The server is giving me back values 1 if the username already exists and 0 if it doesnt. The method is always showing me the error message even if the username is available.
$.validator.addMethod("checkUserName", function(value, element) {
var isAvailable;
$.ajax({
type: "GET",
url: "http://hostname.com/checkUsername",
dataType: "json",
contentType: "application/json; charset=utf-8",
cache: false,
data: {
new_username: function() {
return $("#username").val();
}
},
success: function(data) {
if (data != 0) {
isAvailable = false;
} else {
isAvailable = true;
}
}
});
return isAvailable;
}, "Username already exists!");