File Input accept Attribute
The HTML5 revolution provided us several simple but important attributes like download
, autofocus
, required
, novalidate
, and placeholder
. There's another one you may want to know about: accept
. The accept
attribute is useful for input[type=file]
elements. Let's have a look at it!
The HTML
I'll use Twitter's upload button to illustrate a good usage of the accept
attribute:
<input type="file" name="media_empty" accept="image/gif,image/jpeg,image/jpg,image/png,">
The accept
attribute gets a comma-separated list of mime types for files desired file types. In this case, Twitter is allowing the user to upload common image formats.
If you follow me on Twitter, you know that I've been working on a super top secret mobile application using Appcelerator Titanium. The experience has been great: using JavaScript to create easy to write, easy to test, native mobile apps has been fun. My...
My trip to Mozilla Summit 2013 was incredible. I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out. MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...
A few days back I debuted a sweet article that made use of MooTools JavaScript and opacity to show focus on a specified element. Here's how to accomplish that feat using jQuery.
The jQuery JavaScript
There you have it. Opacity is a very simple but effective...
Without a doubt, my least favorite form element is the SELECT
element. The element is almost unstylable, looks different across platforms, has had inconsistent value access, and disaster that is the result of multiple=true
is, well, a disaster. Needless to say, whenever a developer goes...
The biggest problem about using this code is that the user can change it easily with firebug or others debugs.
It’s not for validation, it’s for assisting the user to select the right file type (since the file selection will only show files of that type).
You should never use this for validation since some browsers do not support it.
Validation should be done on your backend. This kind of selection only helps users what we really need from them.
Be careful with this.
The main issue is that some mobile browsers started to prevent the user from selecting any (!) file at all if the accept property was declared (which is just plain terrible user agent behaviour).
Thus if you want to support these browsers, you have to detect them and remove the accept property.
For details see http://caniuse.com/#feat=input-file-accept