Link to home
Start Free TrialLog in
Avatar of Cyber-Drugs
Cyber-DrugsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Input File Accept Tag

Hi guys,

How would I change this tag so that it only accepts *.doc files?

<input name='myFile' type='file' />
Avatar of ADSLMark
ADSLMark

On the bottom of this page this is explained:

http://www.cs.tut.fi/~jkorpela/forms/file.html

There is a default tage accept="" for the <input> tag, but i tried it with FF and IE and both did not work... So you probably need javascript.. which is explained on the website i gave you..

Mark
<input name='myFile' type='file' accept="image/*"/>

but this does not work on all browsers: http://www.cs.tut.fi/~jkorpela/forms/file.html#filter
Avatar of jessegivy
Welll....  A solution that doesn't work on all browsers isn't really a solution is it?  The javascript validation is a good idea, you could just parse the last three letters of the filename and then not submit the form if it isn't a .doc file, showing an alert box prompting them to select a word document instead.  Might be friendly to display the file extension they're using that's being rejected as well.

I'm baffled that the accept attribute doesn't work, it really pisses me off to be honest, I mean what's the point if it doesn't do what it's supposed to.   ...anyway, the javascript would look like this:

<html>
<head>
<title>Validation Example</title>
<script type="text/javascript">
function validateForm()
{
   var io=document.getElementById("fileIO");
   if(io.value.substring(io.value.length-3)!="doc")
   {
       alert(io.value.substring(io.value.length-4)+" is not a valid file extension.  The file must be a MS Word Document(.doc)");
       return false;
   }
   //if(another element isn't valid)
   //return false;
   //etc....
}
</script>
</head>
<body>
<form onsubmit="validate();">
<input type="file" id="fileIO" />
</form>
</body>
</html>

...please do note that there is no script access given to the value attribute of an input type="file" element.
...sorry, that last line was just to say that the value attribute is readonly, you just can't change it in script.
Avatar of Cyber-Drugs

ASKER

At the moment I am already doing a check to see if the selected file is *.doc or not, but what I am really after is a way to manipulate the form to only allow users to select *.doc files...
ASKER CERTIFIED SOLUTION
Avatar of DreamMaster
DreamMaster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Max,

I just wanted to know if it was possible and if so, how I could do it. If it's not possible, that's fine, I may just create a Flash version, which gives me full control.

Cheers!
Glad to have been helpful :)

Regards,
Max.