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

gene b. picture gene b. · Feb 26, 2016 · Viewed 45.9k times · Source

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?

Answer

Lal picture Lal · Feb 26, 2016

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