I am sure this is probably something simple that i am not doing. Running livevalidation.js jquery plugin (livevalidation.com). It provides for custom function callbacks. I am trying to check for username availability. The server side is working fine and I am getting the proper responses back in my data var...
Here is my JS:
Validate.Username = function(value, paramsObj) {
var paramsObj = paramsObj || {};
var message = paramsObj.failureMessage || "Username is not available";
var isSuccess = true;
$.post("<?php echo fURL::getDomain(); ?>/ajax/username",
function(data) {
if (data.status === 'notavailable')
{
Validation.fail('oops, not available.');
}
});
};
I am calling it using:
var username = new LiveValidation('username', { validMessage: curr_username + "is available!" });
username.add( Validate.Presence, { failureMessage: "Choose a username" });
username.add( Validate.Username, { failureMessage: "Username is not available." } );
The problem I am getting is:
Uncaught ReferenceError: Validation is not defined
If I put the Validation.fail()
outside of my .post()
function it works fine. So am pretty sure it is because it's not able to be referenced inside the .post()
function.
I've tried using a callback function
if (data.status === 'notavailable')
{
status_not_available();
}
I get the same error.
I realize this is something probably extremely simple, but any help would be appreciated. Thank you in advance.
i am having the same issue. Ive found the following, http://forum.jquery.com/topic/ajax-return-value-on-success-or-error-with-livevalidation but have not been able to get it working. BUT YES! At this very moment i made som (crappy) javascript addon that made it behave, i think :)
This is what i use.
function check_avail(name, id, postUrl) { var dataVal = name+'='+$(id).val(); var isaccepted = '' $(id).next('div').remove(); $(id).after("Undersøger om "+name+" er ledigt
"); $.ajax({ url: postUrl, cache: false, type: 'post', dataType: 'json', data: dataVal, async: false, success: function(data) { if( data.success == 'true' ) { $('#'+name+'-availability').remove(); //return false; isaccepted = false; } if( data.success == 'false' ) { $('#'+name+'-availability').remove(); // name.destroy(); isaccepted = true; } } }); if (isaccepted == false) { return false; } else{ return true }; }
And
f1.add( Validate.Custom, { against: function() { return check_avail( 'brugernavn', '#ft001', 'usernamecheck.asp' ); }, failureMessage: 'Brugernavnet er optaget' } );
Hope it helps you :) The json query you can read about on the link in the begining :) (I am not at all skilled at javascript, and the "isaccepted" solution could problalby be made a lot better)