Search code examples
javascriptkeypress

How can I prevent users from using the backspace or delete keys in a textbox using JavaScript?


How can I prevent users from using the backspace or delete keys in a textbox using JavaScript?

I have a text box in my web form. I want to use JavaScript on keypress of the textbox to disable the delete and backspace keys.

Can anyone help?


Solution

  • Why are you letting people edit the contents of the text box but restricting them from being able to use basic editing keys? This sounds like you are creating a major usability issue - what if the user makes a typo?

    I'd recommend either setting the readonly flag (if you don't want users to edit the value) or disabled flag (if you don't even want it submitted)