Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I've a textbox in my web form which opens a popup at click event. I want to prevent users to type any thing and also from using delete and backspace button. I've blocked typing other keys except Delete and backspace. How Can I prevent keypressing of Delete and Backspace?

Try this -

onkeydown="return isNumberKey(event,this)"

function isNumberKey(evt, obj)
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode == 8 || charCode == 46) return false;
    return true;
You can add following java script function for your text box
function onkeyup(e) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (keycode == 8 || keycode == 46)
        return false;
If you want to prevent all input to the textarea, you can set the readonly attribute in the HTML.

If you only want to disable it during the popup, set the textarea element's readOnly member to true at the start of the popup, and reset it to false afterwards.
As others have said, "return false" in your click handler will do it, but take a look at this article[^]. Learn about the event.preventDefault() and event.stopPropagation methods. They are your friend.
Ankur\m/ 3-Jul-10 2:46am
That was very informative. Thanks for posting the link.

PS: I mistakenly posted the comment to the wrong answer. Deleted it! :P

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900