Q:

AJAX Post on Field Completion?

I have the code below that validates a form field called lms_domain. It simply checks whether the subdomain selected by the user is available using an AJAX call to another file that executes a MySQLi count query. The code currently works on submission of the form but I would like it to execute the AJAX post when the user types into the field, or has completed the field. How do you achieve this using JQuery?
$('#lms_name').keyup(function(){

    /* some code here */

    $.ajax({
        type: 'POST',
        url: 'assets/lmsdomaincheck.php',
        data: "lmsdomain="+value+".thedomain.com",
        async: false,
        success: function(htmldata){

            if (htmldata=="success") {
                $('#spanlmsdomain').html('GOOD');
            }else {
                msg+="<b>Error on LMS domain name : </b>"+value+".thedomain.com not available.<br/>";
                $('#spanlmsdomain').html('BAD');
            }
        }
    });

    /* some code here */

});

Thanks in advance for any tips!

A:

$('#lms_name').on('blur', function() {

This will execute once the cursor leaves the field, indicating completion of the user input.
var busy = false;
$('#lms_name').on('blur keyup keypress', function(e) {
    if ((e.type == 'keyup' || e.type == 'keypress') && busy === false) {
        busy = true;
        var avail = setTimeout(function() {
            $.ajax({ ...yourAJAXhere... });
            busy = false;  // Put this line in the success() / error() callbacks
        }, 500);
    } else if (busy === false) {
        clearTimeout(avail);
        busy = true;
        $.ajax({ ...yourAJAXhere... });
        busy = false;  // Put this line in the success() / error() callbacks
    } else {
        return;
    }
});

This will combine the first one with a timeout that waits .5 seconds and then checks the server for availability.  It makes sure only one check goes at a time but is a quick mock up so you would want to fine-tune for your situation.

