简体   繁体   English

用户完成输入后验证用户输入

[英]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. 我正在使用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. 在文本字段上实现onblur事件处理程序,当用户选中下一个字段时将触发该处理程序。

Using jQuery, 使用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. 下面的解决方案解决了这个问题,并在OP请求完成后调用X秒。 It also no longer requires the redundant keydown function. 它也不再需要冗余的keydown功能。 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. 对于最初发布此答案的人来说,只是复制以防有人来到这里(正如我所做)寻找解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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