admin管理员组文章数量:1401241
I'm making a Web application that tests typing speeds.
It gives the user some text to type, and an input box to type into. If the user types a wrong key, I'm using preventDefault
on the produced key event to prevent the wrong character from being entered into the input box (I instead show the user an error message).
The problem is, preventDefault
doesn't prevent backspaces from being entered. Ideally, since wrong keys presses will never be entered into the text box, it doesn't make sense to allow backspacing. If the user habitually hits backspace on a perceived error, it causes the text in the input box bee incorrect. This doesn't affect the results of the test, it's just not an ideal situation.
How can I prevent backspacing in HTML5 input elements of type "text"?
I'm making a Web application that tests typing speeds.
It gives the user some text to type, and an input box to type into. If the user types a wrong key, I'm using preventDefault
on the produced key event to prevent the wrong character from being entered into the input box (I instead show the user an error message).
The problem is, preventDefault
doesn't prevent backspaces from being entered. Ideally, since wrong keys presses will never be entered into the text box, it doesn't make sense to allow backspacing. If the user habitually hits backspace on a perceived error, it causes the text in the input box bee incorrect. This doesn't affect the results of the test, it's just not an ideal situation.
How can I prevent backspacing in HTML5 input elements of type "text"?
Share Improve this question edited Feb 13, 2022 at 15:50 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 8, 2016 at 13:42 CarcigenicateCarcigenicate 45.9k11 gold badges77 silver badges129 bronze badges 2- Do you actually need to prevent backspacing? Another approach may be to detect when it occurs, and then to replace the input element's box with what "should" be there. – Brandin Commented Mar 8, 2016 at 14:29
- @Brandin I don't need to, but I'd like to. And I tried that, but the key press handler fires before the text is placed in the box, so I can't simply replace the back-spaced key, since it's not deleted until after the handler has fired. – Carcigenicate Commented Mar 8, 2016 at 14:53
1 Answer
Reset to default 5You need to detect onkeydown instead of onkeypress and it should work (tested on Firefox/Safari). On some browsers onkeypress is limited to printable characters, whereas onkeydown is for all key down events.
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript">
function no_backspaces(event)
{
backspace = 8;
if (event.keyCode == backspace) event.preventDefault();
}
</script>
</head>
<body>
<input id="typeHere" onkeydown="no_backspaces(event);"/>
</body>
</html>
本文标签: javascriptPrevent backspace in input text boxStack Overflow
版权声明:本文标题:javascript - Prevent backspace in input text box - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744274611a2598350.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论