Disable backspace in textbox via javascript

2019-02-04 19:13发布

I have a textbox which is extended by an Ajax Control Toolkit calendar.

I want to make it so that the user cannot edit the textbox and will have to instead use the calendar extender for input.

I have managed to block all keys except backspace!

This is what I have so far:

<asp:TextBox ID="TextBox1" runat="server" onKeyPress="javascript: return false;" onKeyDown="javascript: return false;" onPaste="javascript: return false;" />

How would I also disable backspace within the textbox using javascript?

EDIT

Made an edit since I need a solution in javascript.

EDIT

It turns out that onKeyDown="javascript: return false;" DOES work. I have no idea why it wasn't working before. I tried using a new textbox and it blocked backspaces fine. So sorry to everyone who posted an answer hoping to get some rep esp. after I marked it for bounty.

My textboxes now (seem) to block ALL keystrokes and also still work with the calendar extender.

10条回答
forever°为你锁心
2楼-- · 2019-02-04 19:34

ReadOnly attribute does not help. The backspace still is taking your browser to back page even if your text box is read only..

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-04 19:45

Can't you just use the HTML readonly="readonly" attribute?

<input type="text" name="country" value="Norway"   readonly="readonly" />

<textarea rows="3" cols="25" readonly="readonly">
It should work! :)
</textarea>
查看更多
仙女界的扛把子
4楼-- · 2019-02-04 19:47

ZX12R was close. This is the correct solution:

The TextBox is like this:

    <asp:TextBox ID="TextBox1" runat="server" onKeyDown="preventBackspace();"></asp:TextBox>

and the script looks like this:

<script type="text/javascript">
    function preventBackspace(e) {
        var evt = e || window.event;
        if (evt) {
            var keyCode = evt.charCode || evt.keyCode;
            if (keyCode === 8) {
                if (evt.preventDefault) {
                    evt.preventDefault();
                } else {
                    evt.returnValue = false;
                }
            }
        }
    }
</script>

First of all, the backspace wont come through on Key Press, so you have to use Key Down.

查看更多
一纸荒年 Trace。
5楼-- · 2019-02-04 19:47

here is a possible solution... add an event listener...

<asp:TextBox ID="TextBox1" runat="server" onKeyPress="KeyCheck;" />

and then the function can be like this..

function KeyCheck(e) {
 var KeyID = (window.event) ? event.keyCode : e.keyCode;
 if (KeyID == 8 ) {
    alert('no backspaces!!);
 }
}

doubt if it has to be onkeypress or onkeyup...

查看更多
聊天终结者
6楼-- · 2019-02-04 19:47

use regular text boxes not read-only and not Disabled, just use client-side JavaScript to ignore keypresses. This will ignore all keypress so you will have your READONLY behaviour and it will also ignore the backspace.

<input type="text" value="Your Text Here" onkeydown="return false;" />
查看更多
三岁会撩人
7楼-- · 2019-02-04 19:47

No Need to call any function and all just try this:

<asp:TextBox runat="server" ID="txt" onkeydown="return false;" 
    onpaste = "return false;" onkeypress="return false;" />
查看更多
登录 后发表回答