0

Hi there,

I'm trying to do a Username validation with JQuery but I'm not sure how to get a "TRUE" from the first section of code before going to the next section which is an ajax call to the backend. Below is my code.

$(document).ready(function() {
    $("#username").change(function() {
        var username = $("#username").val(); //Get the value in the username textbox
        
        //How do you get a true/false from here?
          
        username.validate({
            rules: {
                username: {
                    required: true,
                    minlength: 2
                }
            },
            messages: {
                username: {
                    required: "Please specify your name",
                    minlength: "Your username should be at least 2 characters"
                }
            }
        });
        
        //This part should not process until the code above returns true

        $("#availability_status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...'); //Add a loading image in the span id="availability_status"
        $.ajax({    //Make the Ajax Request
            type: "POST",
            url: "username_availability.php", //file name
            data: "username=" + username, //data
            success: function(server_response) {
                $("#availability_status").ajaxComplete(function(event, request) {
                    if(server_response == '0') { //If ajax_check_username.php return value "0"
                        $("#availability_status").html('<img src="available.png" align="absmiddle"> <font color="Green"> Available </font>');
                    //Add this image to the span with id "availability_status"
                    }
                    else  if(server_response == '1') { //if it returns "1"
                        $("#availability_status").html('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
                    }
                });
            }
        });

        return false;
    });
});

Thanks for your Skillz!

2
Contributors
2
Replies
6
Views
6 Years
Discussion Span
Last Post by AshtonHogan
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.