简体   繁体   中英

validating user input when user finished typing

what is the best way to validate user input for aa text field when the user is done typing? I am using javascript/jquery. is there a method that knows when the user has clicked out of the text field?

Implement onblur event handler on the textfield which will be triggered when the user tabs out to next field.

Using jQuery,

$('.input_selector').on ('blur', function() {
    var inpValue = $(this).val();
    //validate Code
});

For all those who came here looking for a solution, please check this answer .

Basically:

The solution below solves this problem and will call X seconds after finished as the OP requested. It also no longer requires the redundant keydown function. I have also added a check so that your function call won't happen if your input is empty.

//setup before functions
var typingTimer;                //timer identifier
var doneTypingInterval = 5000;  //time in ms, 5 seconds for example

//on keyup, start the countdown
$('#myInput').keyup(function(){
    clearTimeout(typingTimer);
    if ($('#myInput').val()) {
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    }
});

//user is "finished typing", do something
function doneTyping() {
    //do something 
}

All credit for the guy who originally posted this answer, just copying in case somebody arrived here (as I did) looking for a solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM