form validation combined with ajax
I'm trying to make a username form validation combined with ajax but I'm stuck at 1 point. I need to use a var that is outside of my $.get() function so that I can add the function to the validate plugin.
This is what I have:
- jQuery.validator.addMethod("username", function(value, element) {
- var result = false;
- $.get("includes/checkUsername.php?user="+value, function(data){
- if(data == "0") {
- result = true;
- }
- });
- return result;
- }, "Username not available");
As you can see, I'm trying to change the value of "var result" to true, but since that is outside of my $.get() it doesn't seem to happen. I tried to put the return command inside the $.get function, but then my validator always keep saying it's a false check.
Is there anyway I can call that var inside my $.get function?