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.
Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it?
At first blush, it was the simplicity of it as compared to the table-and-spacer...
I love almost every part of being a tech blogger: learning, preaching, bantering, researching. The one part about blogging that I absolutely loathe: dealing with SPAM comments. For the past two years, my blog has registered 8,000+ SPAM comments per day. PER DAY. Bloating my database...
Overlays have become a big part of modern websites; we can probably attribute that to the numerous lightboxes that use them. I've found a ton of overlay code snippets out there but none of them satisfy my taste in code. Many of them are...
A goal of my latest blog redesign was to practice what I preached a bit more; add a bit more subtle flair. One of the ways I accomplished that was by using CSS3 animations to change the display of my profile icons (RSS, GitHub, etc.) I...
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