Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent typing in Text Field Input, Even Though Field is NOT Disabled/Read-Only

Tags:

Can I prevent HTML Text Field input even when my field is NOT Disabled or Read-only? I have this requirement.

Maybe I can block all inputs via JS or jQuery?

like image 211
gene b. Avatar asked Feb 26 '16 18:02

gene b.


People also ask

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

To prevent user from typing in text field without disabling the field with HTML, we can add the readonly attribute to the input. to stop users from enter text into the input without making it look disabled.

How do I prevent user input in a text box?

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

How do you restrict input fields?

To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

How do I restrict a number in text field?

Using <input type="number"> The standard solution to restrict a user to enter only numeric values is to use <input> elements of type number. It has built-in validation to reject non-numerical values.


2 Answers

See this fiddle

You can use jQuery for this.. You can do it as below

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

OR

you can just return false instead of using preventDefault(). See the script below

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

See the fiddle

OR

A much simplified version without Javascript would be as below. Just change your HTML as below

<input type="text" onkeypress="return false;"/> 

See the fiddle

like image 61
Lal Avatar answered Sep 22 '22 19:09

Lal


If you wish to make the field complete in-intractable.

$('input').focus(function(e) {     $(this).blur(); }); 

Example : https://jsfiddle.net/DinoMyte/up4j39qr/15/

like image 28
DinoMyte Avatar answered Sep 24 '22 19:09

DinoMyte