Jacob
Jacob

Reputation: 1123

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

I tried:

$('input').keyup(function() {

   $(this).attr('val', '');

});

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?

Upvotes: 112

Views: 226119

Answers (12)

kuubson
kuubson

Reputation: 189

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

  onFocus={e => e.target.blur()}

Upvotes: 2

Stephen L
Stephen L

Reputation: 2339

For a css-only solution, try setting pointer-events: none on the input.

Upvotes: 2

Mystical
Mystical

Reputation: 2783

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

<input value="CAN'T ADD TO THIS" maxlength="0" />

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:

<input value="THIS IS READONLY" onkeydown="return false" />

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

Upvotes: 11

JKS
JKS

Reputation: 3770

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 = '';
});
<input type="text" />

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

Upvotes: 1

anandd360
anandd360

Reputation: 306

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

    <asp:TextBox ID="txtDate" runat="server" AutoPostBack="True"
ontextchanged="txtDate_TextChanged" onkeydown="return false" >
    </asp:TextBox>

Upvotes: 1

irJvV
irJvV

Reputation: 912

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

//.attr(function(){if(condition){"readonly"]else{""}) //INCORRECT CODE !

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

var d = document.getElementById("div1");
d.className += " readonly";

OR in D3:

 .classed("readonly", function(){
   if(condition){return true}else{return false}
 })

AND add to CSS or less:

.readonly {
  pointer-events: none;
}

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/,'');

or use Jquery:

$( "div" ).removeClass( "readonly" )

or toggle the class:

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

Just to be complete, good luck =^)

Upvotes: 1

Mike Mertsock
Mike Mertsock

Reputation: 12015

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. <input readonly type="text" ...>

Upvotes: 240

user2734846
user2734846

Reputation:

Markup

<asp:TextBox ID="txtDateOfBirth" runat="server" onkeydown="javascript:preventInput(event);" onpaste="return false;"
                                TabIndex="1">

Script

function preventInput(evnt) {
//Checked In IE9,Chrome,FireFox
if (evnt.which != 9) evnt.preventDefault();}

Upvotes: 1

Art
Art

Reputation: 1340

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

Upvotes: 77

qw3n
qw3n

Reputation: 6334

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

Upvotes: 3

treecoder
treecoder

Reputation: 45081

$('input').keydown(function(e) {
   e.preventDefault();
   return false;
});

Upvotes: 36

Derek Hunziker
Derek Hunziker

Reputation: 13141

$('input').keypress(function(e) {
    e.preventDefault();
});

Upvotes: 20

Related Questions