8

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.

Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
Matt McCormick
  • 13,041
  • 22
  • 75
  • 83

9 Answers9

7

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

<input type="file" name="file" onKeyDown="this.blur()" onContextMenu="return false;">
rnielsen
  • 700
  • 1
  • 5
  • 11
2

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.

Ben S
  • 68,394
  • 30
  • 171
  • 212
  • not on the server, it should be on the browser... if the HTML design were saner, of course. – Javier Feb 26 '09 at 19:15
  • 1
    Users can tamper with the HTML/Javascript and still submit files that doesn't conform to what the web page checks. The only way to ensure it is to check it server-side. A client-side would still be good to save non-tampering users some time and server resources. – Ben S Feb 26 '09 at 19:27
2

I solved this with another way using a button rather than a submit and using JavaScript to check the value before submitting.

<input type="file" name="inputFile">
<input type="button" onclick="if(fileHasValidPath()) { submitForm(); }" value="Submit">

function fileHasValidPath() {
  if (isIE()) {
    var inputFile = document.forms[0].inputFile;
    if (inputFile.value != "" && /^(\w:)|(\\)/.test(inputFile.value)) {
      alert("File is not a valid file.  Please use the Browse button to select a file.");
      inputFile.select();
      inputFile.focus();
      return false;
    }
  }

  return true;
}

function submitForm() {
  document.forms[0].submit();
}

I realise there still needs to be server-side validation for the file but this is only to prevent a user clicking on the Submit button and not seeing anything happening. Also, it assumes IE using a Windows OS.

Matt McCormick
  • 13,041
  • 22
  • 75
  • 83
  • You could also add the validation method to the form's onsubmit event and use a standard submit. – Ishmael Feb 26 '09 at 21:56
  • 2
    "Also, it assumes IE using a Windows OS." -> I can't count the time I've spent cursing web sites that make these assumptions... – Jeff Barger Apr 14 '09 at 23:44
1

the input type=file element's value property is readonly as for security issue. You can read this property and do a check. If it is not correspond with your rules, you will give a warn and let person to modify it. The other way use outerHTML property override it. for example: HTML input file element called objInputFileElement. objInputFileElement.outerHTML="";

yuxiaoxu
  • 11
  • 1
1

one way is to put a little javascript code in the buttons onsubmit. The idea being to validate the box and either stop submission or allow it.

However, you are probably better off just validating the file contents server side and rendering the appropriate error back to the client.

NotMe
  • 87,343
  • 27
  • 171
  • 245
0

I found the option that solves your issue. Is the contentEditable option as follows:

<input type="file" name="name" contentEditable="false"/>
Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
0

Here is a demonstration of the contentEditable option:

<input type="file" name="name" contentEditable="false" />
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
0

I based my solution on this thread.

So, considering this file field:

<input type="file" id="file_field">

I got it to work with something like this in jquery:

$(document).ready( function() {
    $('#file_field').bind("keydown", function(e) {
        e.preventDefault();
    });
});

NOTE: You must use keydown instead of keypress as it covers DEL and BACKSPACE as well as any print key.

Community
  • 1
  • 1
Dentra Andres
  • 371
  • 1
  • 7
  • 18
0

Couldn't you use

<input ... disabled>

EDIT:no, actually that prevents submission as well... but in HTML 4 apparently

<input ... readonly>

should work. http://htmlhelp.com/reference/html40/forms/input.html

David Z
  • 128,184
  • 27
  • 255
  • 279
  • This disables the user from actually clicking the "Browse" button to select a file. I still want them to be able to select a file. – Matt McCormick Feb 26 '09 at 18:05