Disable backspace in textbox via javascript

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?


Made an edit since I need a solution in javascript.


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.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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) {
                } else {
                    evt.returnValue = false;

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

Method 2

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! :)

Method 3

How about using a label for the display and a hidden textbox to get the value back to the server?

Method 4

You need to apply readonly on the client side controller ONLY, so that asp.net doesn’t see it and still reads the data on postback. You can do this several ways, one of the easier if you use jQuery is to add a class to the text-boxes eg. cssclass="readonly" in question and $(".readonly").attr("readonly", true);.

Method 5

As others said ReadOnly=”True” will break the postback mechanism.

I believe you can get around it in your code-behind by accessing the Request object directly during PageLoad:

//assuming your textbox ID is 'txtDate'
   this.txtDate.Text = Request[this.txtDate.UniqueID];

Your other option is to allow Disabled controls to postback on the form, but this is somewhat of a security concern as readonly fields modified via script could potentially come back:
<form id="MyForm" runat="server" SubmitDisabledControls="True">


I’m not sure the impact of this property on ReadOnly (vs Enabled=”False”) controls but it’s worth trying.

And finally – I did run into the same issue you’re having a few years ago, and from what I remember there is a difference between using an html input marked as readonly and runat=”server”, and an actual serverside control where ReadOnly=”true”.

I have a feeling doing:

<input type="text" readonly="readonly" runat="server" id="myTextBox" />

may have still allowed the data to come through, although in the code-behind you have to treat the control as a HtmlInputText or HtmlGenericControl vs. a TextBox. You can still access the properties you need though.

Just a few ideas anyway…

Method 6

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…

Method 7

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

Method 8

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;" />

Method 9

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;" />

Method 10

I was able to do something similar, with Jquery. Just putting it out here for reference!

$("#inputID").keypress(function (e)

$("#inputID").keydown(function (e)

the first prevents keypresses, while the second prevents key down events.

Still a JS noob, so feel free to point out good / bad things with this.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x