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
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • By
    Fancy Navigation with MooTools JavaScript

    Navigation menus are traditionally boring, right? Most of the time the navigation menu consists of some imagery with a corresponding mouseover image. Where's the originality? I've created a fancy navigation menu that highlights navigation items and creates a chain effect. The XHTML Just some simple...

  • By
    Duplicate DeSandro&#8217;s CSS Effect

    I recently stumbled upon David DeSandro's website when I saw a tweet stating that someone had stolen/hotlinked his website design and code, and he decided to do the only logical thing to retaliate:  use some simple JavaScript goodness to inject unicorns into their page.

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!