如何在另一个函数中使用各种ajax请求的结果?

I have been programming a registration form with ajax validation. The way I have it set up is in my js file, I have listeners that fire when the content of the field is changed. They send the data to the server, and the server makes sure it's valid and sends back its response in the form of a JSON object. I then read the values of the JSON object to output potential error messages.

I won't copy and paste the entire files, just one example:

$(document).ready(function() {
    // USERNAME VALIDATION LISTENER

    $("#regUsername").change(checkName);
}

and then the checkName function looks like this, it sends my ajax request:

function checkName() {
    $.ajax({
        type: "POST",
        url: "./ajax_register.php",
        data: {
            request: "nameAvail",
            username: $("#regUsername").val()
        },
        success: function(data) { // execute on success
            var json = jQuery.parseJSON(data);

            if (json.success) { // if usernames do match
                $("#usernameAvailiability").removeClass().addClass('match');
                $("#usernameAvailiability").text(json.msg);  
            } else { // if the user has failed to match names
                $("#usernameAvailiability").removeClass().addClass('nomatch');
                $("#usernameAvailiability").text(json.msg);
            }
        }
    });
}

And depending on the response, it updates a span that tells the user if the input they wrote is valid or not. The server validates with this part of the php file:

if(!isset($_POST['request'])) { // do nothing if no request was provided
    print("no request provided");
} else { //ELSE request has been provided
    if ($_POST['request'] == "nameAvail") { // if the request is to check if the username is valid
    $response = array("success" => false, "msg" => " ", "request" => "nameAvail");

    // CHECK USER NAME AVAILIABILITY CODE

    if (!isset($_POST['username']) || empty($_POST['username'])) { // if no username is entered
        $response['success'] = false;
        $response['msg'] = "No username provided";
    } else { // if a username has been entered
        $username = $dbConn->real_escape_string($_POST['username']);

        if (!ctype_alnum($username)) { // Make sure it's alpha/numeric
            $response['success'] = false;
            $response['msg'] = "username may only contain alpha numeric characters";
        } elseif (strlen($username) < 4) { // make sure it's greater than 3 characters
            $response['success'] = false;
            $response['msg'] = "username must be at least 4 characters long.";
        } elseif (strlen($username) > 20) { // make sure it's less than 26 characters
            $response['success'] = false;
            $response['msg'] = "username can be up to 20 characters long.";
        } else { // make sure it's not already in use
            $query = $dbConn->query("SELECT `id`, `username` FROM `users` WHERE `username` = '"
                    . $username . "' LIMIT 1");

            if ($query->num_rows) { // if the query returned a row, the username is taken
                $response['success'] = false;
                $response['msg'] = "That username is already taken.";
            } else { // No one has that username!
                $response['success'] = true;
                $response['msg'] = "That username is availiable!";
            }
        }
    }
    print(json_encode($response));
}

What I'd like to do now is create a function in my javascript for the register button. But I need to make sure all the forms are validated first.

I'm not sure what my options are. What I'd LIKE to do is somehow be able to recycle the code I've already written in my PHP file. I don't want to write out an entirely new if($_POST['request'] == "register") clause and then copy and paste all the validation code to make sure the input is valid before I insert the registrant's data into the database. It seems really repetitive!

I know I could check to see if all the spans on the page were set to 'match', but that could easily be tampered with and blank forms could be submitted.

so far, my register button function looks like this:

function register() { 
    if ( NEED SOME KIND OF CLAUSE HERE TO CHECK IF ALL THE FIELDS ARE VALID) {

    $.ajax({
        type: "POST",
        url: "./ajax_register.php",
        data: {
            request: "register",
            username: $("#regUsername").val(),
            password: $("#regPassword").val(),
            email: $("#email").val(),
            dob: $("#dob").val(),
            sQuest: $("#securityQuestion").val(),
            sAns: $("#securityAnswer").val(),
            ref: $("#referred").val()
        }, success: function(data) {
            var json = jQuery.parseJSON(data);
            console.log(json);

            $("#regValid").removeClass();
            $("#regValid").text("");
        }
    }); //AJAX req done

} else {
    $("#regValid").removeClass().addClass('nomatch');
    $("#regValid").text("One or more fields are not entered correctly");
}
return false;// so that it wont submit form / refresh page
}

I would really appreciate some help, I've spent the last few hours scouring StackOverflow for an answer, but I can't seem to get anything to work. Will I have to duplicate code in my PHP file or is there a more elegant way to handle this?