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?
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.
Setting the onkeydown attribute to return false makes the input ignore user keypresses on it, thus preventing them from changing or affecting the value.
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.
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.
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
If you wish to make the field complete in-intractable.
$('input').focus(function(e) { $(this).blur(); });
Example : https://jsfiddle.net/DinoMyte/up4j39qr/15/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With