Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block writing in input text?

Tags:

javascript

I just creating a booking system and I want that the users will choose a date from the calender and it'll show in input text - that I did!

Now I want to block the writing in the input text (just bring to calender writes there when the user choose a date). How do I do it? I guess JavaScript, but how? I dont know JS very wall..

Thank you very much!

like image 801
Luis Avatar asked Aug 03 '10 19:08

Luis


People also ask

How do you prevent a user from typing in an input field?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable.

How do I disable editing in input tag?

You can either use the readonly or the disabled attribute. Note that when disabled, the input's value will not be submitted when submitting the form. Also It's important to remind that even using readonly attribute, you should never trust user input which includes form submissions.

How do I make a text input non editable for a part of it?

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.

How do you style text inside input field?

Styling Input Fields If you only want to style a specific input type, you can use attribute selectors: input[type=text] - will only select text fields. input[type=password] - will only select password fields. input[type=number] - will only select number fields.


2 Answers

Give your element the readonly attribute, this will disallow users to type anything into it. However, you will still be able to write to add through javascript for example when a date is chosen. Here is an example:

<input type="text" id="txt" readonly="readonly">

JavaScript:

var el = document.getElementById('txt');
el.value = "Testing......";

Working Demo

like image 159
Sarfraz Avatar answered Sep 22 '22 13:09

Sarfraz


<input type="text" id="someId" disabled="disabled" />

The disabled property will prevent any user input, but you can still write to it via your javascript calendar method.

like image 34
Marko Avatar answered Sep 23 '22 13:09

Marko