ajax return value on success or error with LiveValidation
I am new here, but not new to classic javascript and server-side programming. My server-side Ruby on Rails code returns a 200 OK header if the login name WAS NOT found - meaning that you are OK to use what you typed in. If the login name WAS found, the server returns 500 Error. This part works as confirmed by the console.log(....) entries.
What doesn't work is my function check_login() won't return the true or false in the "success" and "error" sections of the ajax call.
I know there has got to be a simple solution - but I have been trying all sorts of things for hours now with NO success.
My server-side logic is clean, the livevalidation works great...
http://www.livevalidation.com/documentation#ValidateCustom
If I could get my function check_login() to return true on success an false on error my code would be golden!
any help you can provide would be awesome!
thanks for reading my post,
Andy
-
function check_login() {
var login = $('input#user_login').val();
$.ajax({
url : '<%= check_login_path %>',
data : "login=" + escape( login ),
success: function () {
console.log("passed - that's a new login!");
return true; // unique, go ahead and use it
},
error: function() {
console.log("failed - that login is taken!");
return false; // cannot use it because it is already in use
}
});
}
$(document).ready( function() {
var login_field = new LiveValidation('user_login', { validMessage: 'valid login', wait: 400 });
login_field.add( Validate.Format, { pattern: /^[\w][\w\-\.]+$/i, failureMessage: 'invalid format' } );
login_field.add( Validate.Custom, { against: function() { return check_login(); }, failureMessage: 'already in use' } );
}