File Input accept Attribute

By  on  

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.

Recent Features

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

  • By
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

Incredible Demos

  • By
    dwImageProtector Plugin for jQuery

    I've always been curious about the jQuery JavaScript library. jQuery has captured the hearts of web designers and developers everywhere and I've always wondered why. I've been told it's easy, which is probably why designers were so quick to adopt it NOT that designers...

  • By
    CSS Ellipsis Beginning of String

    I was incredibly happy when CSS text-overflow: ellipsis (married with fixed width and overflow: hidden was introduced to the CSS spec and browsers; the feature allowed us to stop trying to marry JavaScript width calculation with string width calculation and truncation.  CSS ellipsis was also very friendly to...

Discussion

  1. The biggest problem about using this code is that the user can change it easily with firebug or others debugs.

    • Matthew

      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.

  2. 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

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!