Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 304
Jacob Avatar asked Aug 05 '11 05:08

Jacob


People also ask

How do I make a text field not editable?

The readonly attribute makes a form control non-editable (or “read only”). A read-only field can't be modified, but, unlike disabled , you can tab into it, highlight it, and copy its contents. Setting the value to null does not remove the effects of the attribute. Instead use removeAttribute('readonly') .

How do I restrict a text box?

-]+\. [a-z]{2,3}$"> will restrict the allowed characters according that RegExp pattern (in this case: valid-looking email addresses). The title attribute will be used as a warning / notification when the user tries to submit the data not matching the requirement.

How do I make a text field read only?

The readonly attribute is a boolean attribute. When present, it specifies that an input field or textarea is read-only. A read-only field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it).

How do I hide text field?

The <input type="hidden"> defines a hidden input field.


2 Answers

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" ...>

like image 146
Mike Mertsock Avatar answered Sep 28 '22 04:09

Mike Mertsock


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

like image 21
Art Avatar answered Sep 28 '22 05:09

Art