有没有什么方法可以让我在输入框中输入然后在离开该框后,JavaScript会自动检查输入框中的输入。?
就像我们在gmail或facebook中创建帐户一样,当我们输入我们的名字时,它会自动检查名称... 与正则表达式的javascript相同..是文本还是数字?
答案 0 :(得分:0)
猜猜你在寻找这样的东西:
var timeout;
function callValidate() {
// clear timeout if any key is pressed before validate is executed (cancel execution of validate function)
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
// since there is no way to tell when/if someone stopped typing
// wait 0.8 seconds before validation is executed
timeout = setTimeout(validation, 800)
}
function validation() {
// do the validation eg:
var name = document.getElementById('name');
console.log(!isNaN(parseFloat(name.value)) && isFinite(name.value) ? 'Number entered...' : 'Text entered...');
}
您的输入字段应如下所示:
<input type="text" id="name" onkeyup="callValidate()">