How to prevent user from typing in text field without disabling the field?

but it removes the entered text slightly after a letter is entered. Is there anyway to prevent the user from entering text completely without resorting to disabling the text field?

146k 31 31 gold badges 178 178 silver badges 212 212 bronze badges asked Aug 5, 2011 at 5:03 1,113 2 2 gold badges 7 7 silver badges 4 4 bronze badges is keydown() or keypress() any more effective? Commented Aug 5, 2011 at 5:06

12 Answers 12

A non-Javascript alternative that can be easily overlooked: can you use the readonly attribute instead of the disabled attribute? It prevents editing the text in the input, but browsers style the input differently (less likely to "grey it out") e.g.

answered Aug 5, 2011 at 5:07 Mike Mertsock Mike Mertsock 12k 8 8 gold badges 43 43 silver badges 76 76 bronze badges

The readonly attribute does not have a value, it just needs to be present in both HTML 4.01 and HTML5. The only time it needs a value is in valid XHTML documents (which are extremely rare on the web).

Commented Aug 5, 2011 at 5:20

That's exactly correct RobG, I should have mentioned that. Also want to mention that this attribute should work for textarea elements, and I think it works for checkbox/radio inputs as well.

Commented Aug 5, 2011 at 5:22

There is one downside to this method. The user can still click into the field, making it appear as if it's editable. If they hit backspace to remove what's in there, it may trigger the "back" functionality of the browser. Not a good experience.

Commented Jan 24, 2013 at 0:59

If there are concerns about the user experience or if the default styling of a readonly field is just plain ugly, take the progressive enhancement approach and use CSS to make it more obvious that it's not editable: jsfiddle.net/eBh5N

Commented Jan 24, 2013 at 13:07

@MikeMertsock nice one. this helped me where disabled suppressed the click event, but using readonly the click event still works.

Commented Dec 15, 2022 at 21:46

if you don't want the field to look "disabled" or smth, just use this:

onkeydown="return false;" 

it's basically the same that greengit and Derek said but a little shorter

answered Jul 3, 2013 at 11:43 1,320 13 13 silver badges 26 26 bronze badges I had to e.preventDefault(); in addEventListener Commented Apr 16, 2015 at 1:35 even shorter: onkeydown="return false" Commented Jan 7, 2016 at 13:24 Just be aware that this does not prevent the user from pasting text with the mouse. Commented Apr 16, 2016 at 13:50

This is perfect when using a datepicker for example, to prevent the user from typing the date and actually use the datepicker. But as @gsanta said, they will still be able to paste text, so just make sure to have a back-end validation aswell :)

Commented Sep 23, 2016 at 9:41

On an ASP.NET website, setting a text box to readonly causes the text to be lost on a post back. This is the better solution in such cases.

Commented Dec 1, 2016 at 13:02
$('input').keydown(function(e) < e.preventDefault(); return false; >); 
answered Aug 5, 2011 at 5:06 44.6k 23 23 gold badges 70 70 silver badges 93 93 bronze badges

actually, I have a date of birth field and I want to prevent this from user typing, so for me your answer is GOOD!! If I use "readonly" attribute then user will not able to click on input and DOB popup coming through Click on input.. :) CHEERS. and Thanks

Commented Nov 10, 2014 at 6:24

@treecoder: Good example. But what if I want to include a conditional check before preventing input? For example, I want to prevent input of all characters from "a" through "z", both in lowercase and uppercase, and the "@" symbol. All other symbols and numbers should be allowed. How do I do that using specifically the jQuery function you wrote above?

Commented Dec 18, 2021 at 13:08
$('input').keypress(function(e) < e.preventDefault(); >); 
answered Aug 5, 2011 at 5:06 Derek Hunziker Derek Hunziker 13.1k 4 4 gold badges 58 58 silver badges 107 107 bronze badges This will only prevent input, while you can still use the arrow keys and so on. Thank you! Commented Jan 7, 2016 at 13:23

If you want to prevent the user from adding anything, but provide them with the ability to erase characters:

Setting the maxlength attribute of an input to "0" makes it so that the user is unable to add content, but still erase content as they wish.

But If you want it to be truly constant and unchangeable:

Setting the onkeydown attribute to return false makes the input ignore user keypresses on it, thus preventing them from changing or affecting the value.

answered Jan 25, 2019 at 20:00 2,744 2 2 gold badges 25 25 silver badges 44 44 bronze badges onKeyDown does not prevent from copy-paste or drag the contents Commented Jun 25 at 12:43

One other method that could be used depending on the need $('input').onfocus(function()); I think this is how you would write it. I am not proficient in jquery.

answered Aug 5, 2011 at 5:17 6,294 6 6 gold badges 34 34 silver badges 63 63 bronze badges

The best solution is to unfocus input once user clicks it so it makes it kinda readonly

 onFocus= e.target.blur()> 
answered Jan 21, 2022 at 10:47 191 2 2 silver badges 3 3 bronze badges

While this code may answer the question, it would be better to explain how it solves the problem without introducing others and why to use it. Code-only answers are not useful in the long run.

Commented Jan 21, 2022 at 17:52

See "Explaining entirely code-based answers". While this might be technically correct it doesn't explain why it solves the problem or should be the selected answer. We should educate in addition to help solve the problem.

Commented Jan 21, 2022 at 22:57
function preventInput(evnt) < //Checked In IE9,Chrome,FireFox if (evnt.which != 9) evnt.preventDefault();>
answered Jun 22, 2015 at 5:32 user2734846 user2734846 This will help you to implement tab functionality in your DatePicker Filed and prevent editing. – user2734846 Commented Jun 22, 2015 at 5:33

I like to add one that also works with dynamic javascript DOM creation like D3 where it is impossible to add:

//.attr(function()) //INCORRECT CODE ! 

to prevent actions on a HTML input DOM element add readonly to class:

var d = document.getElementById("div1"); d.className += " readonly"; 
 .classed("readonly", function()< if(condition)else >) 

AND add to CSS or less:

.readonly

the nice thing about this solution is that you can dynamically turn it on and of in a function so it can be integrated in for example D3 at creation time (not possible with the single "readonly" attribute).

to remove the element from class:

document.getElementById("MyID").className = document.getElementById("MyID").className.replace(/\breadonly\b/,''); 
$( "div" ).removeClass( "readonly" ) 

or toggle the class:

$( "div" ).toggleClass( "readonly", addOrRemove ); 

Just to be complete, good luck =^)

answered Feb 26, 2016 at 9:23 902 8 8 silver badges 27 27 bronze badges

just use onkeydown="return false" to the control tag like shown below, it will not accept values from user.

answered Apr 7, 2016 at 7:45 306 4 4 silver badges 15 15 bronze badges

One option is to bind a handler to the input event.

The advantage of this approach is that we don't prevent keyboard behaviors that the user expects (e.g. tab, page up/down, etc.).

Another advantage is that it also handles the case when the input value is changed by pasting text through the context menu.

This approach works best if you only care about keeping the input empty. If you want to maintain a specific value, you'll have to track that somewhere else (in a data attribute?) since it will not be available when the input event is received.

const inputEl = document.querySelector('input'); inputEl.addEventListener('input', (event) => < event.target.value = ''; >);

Tested in Safari 10, Firefox 49, Chrome 54, IE 11.