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
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • By
    5 Awesome New Mozilla Technologies You&#8217;ve Never Heard Of

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

Incredible Demos

  • By
    Create a Simple News Scroller Using MooTools, Part I:  The Basics

    News scroller have been around forever on the internet. Why? Because they're usually classy and effective. Over the next few weeks, we'll be taking a simple scroller and making it into a flexible, portable class. We have to crawl before we...

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

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.

  • 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!