Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable file input text box in IE?

Is it possible to prevent a user from typing in a file input text box in IE? The reason I ask is that if a user enters text that does not look like a file system path (eg. doesn't start with something like c:...) then when the user clicks the submit button nothing will happen.

I would either like to not allow the user to type in the box or have the form submit as normal.

I have found that the same question was asked here with no answer: http://www.webmasterworld.com/html/3290988.htm

And this person came up with a hack which I can use if there is no other suitable answer: http://www.shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom

EDIT: To clarify - if the user types "not a file path" in the text box next to the "Browse" button and clicks submit, in IE nothing will happen. The form will not submit - IE does not allow a form to be submitted when a <input type="file"> box does not have a real file path.

like image 200
Matt McCormick Avatar asked Feb 26 '09 18:02

Matt McCormick


People also ask

How do I disable a text box?

We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.

How do I remove the input from a file?

To reset a file input in React, set the input's value to null in your handleChange function, e.g. event. target. value = null . Setting the element's value property to null resets the file input.


2 Answers

How about this one? You can't type, or right click and paste.

<input type="file" name="file" onKeyDown="this.blur()" onContextMenu="return false;">
like image 137
rnielsen Avatar answered Sep 19 '22 22:09

rnielsen


This may be a bad idea to begin with. What if the user is not using a Windows OS and wants to upload the file /home/user/example.txt?

This type of check might be better implemented server side.

like image 44
Ben S Avatar answered Sep 20 '22 22:09

Ben S