简体   繁体   English

当我长按键盘上的一个键时,事件输入错误页面

[英]Event input bug page when i long press a key on keyboard

the problem i have is in a input event, when i write my email, i have a function with input event who check if the e-mail is valid or no.我遇到的问题是在一个输入事件中,当我写我的 email 时,我有一个 function 带有输入事件,它检查电子邮件是否有效。 So when is not valid, i use a innerHTML for show a message invalid email.因此,当无效时,我使用 innerHTML 来显示无效 email 的消息。 The problème is when i long press a key on my keyboard, my else is every time executed and my page slow down and bug?问题是当我长按键盘上的一个键时,每次都执行我的 else 并且我的页面变慢并出现错误? How can i resolve this problem please: This is my JS code :请问我该如何解决这个问题:这是我的JS代码:

let newsletter_email = document.getElementById('newsletter_email');

newsletter_email.addEventListener('input', function(e) {
    let info_newsletter = document.getElementById('info_newsletter');

    if(confirmEmail(newsletter_email.value)){
        info_newsletter.innerHTML = "<h6 style='color: rgb(1, 196, 1);'>E-Mail valide !</h6>";
        let interval_valid_email = setTimeout(function() {
            info_newsletter.innerHTML = "";
        }, 2000);
    }else {
        info_newsletter.innerHTML = "<h6 style='color:red;'>E-Mail non valide !</h6>";
        e.preventDefault();
    }
});

Thank u for help !谢谢你的帮助!

I advice you to use blur event我建议你使用模糊事件

Example:例子:

newsletter_email.addEventListener('blur', (e) => {
    // Logic to check the email
});

So in this case, you check the email when the focus is lost.因此,在这种情况下,您在失去焦点时检查 email。 It's not usefull check the email when the user press a single character.当用户按下单个字符时,检查 email 是没有用的。

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

相关问题 在移动模式下如何处理按键事件。 当在输入类型=“文本”上按下键时..在桌面模式下正常工作(通过键盘按下键) - how to handle on key press event on mobile mode . when key press on input type=“text”.. its work proper on desktop mode (key press by keyboard) 当我按下一个键时,事件 Javascript 有声音 - Event Javascript with sound when I press a key Android WebView 中的键盘长按键 - Keyboard Long Key Press in Android WebVIew 当我按键盘上的键时,keydown事件会触发多次 - keydown event fire multiple times when i press keys on keyboard 当我按下键盘上的一个键时如何使用 console.log? - How to console.log when I press a key in the keyboard? 当我用javascript在键盘上按箭头键时增加数字 - increase number when I press arrow key on keyboard with javascript 长按链接时jQuery Mobile Bug? - jquery Mobile bug when long press link? 检查Javascript中的文本区域输入时的按键事件监听器 - Key-Press event listener when checking textarea input in Javascript 只有当我按下输入中的键时,反应事件 onKeyDown 才有效 - React event onKeyDown only works if I press the key inside an input 即使按下键盘上的Enter键,如何调用按钮按下事件 - How to call a Button press Event even with the press of the Enter key on keyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM