javascript - Prevent backspace in input text box - Stack Overflow

admin2025-04-10  0

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
Add a ment  | 

1 Answer 1

Reset to default 5

You 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>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744274614a239185.html

最新回复(0)