Mike Trpcic Mike Trpcic - 2 years ago 81
HTML Question

HTML file input field with limited file types?

If I have an HTML file input field, is there any way that I can limit it to only allow image uploads? I'm sure this would be possible with Flash or Java, but I'm trying to stay away from them for the time being.

I found the "accept" attribute online, but it doesn't seem to be doing anything for me (or I'm using it wrong.). Any help or examples is appreciated.

Answer Source

The best way is to handle that in the backend. Depending on the type of types you want to allow and the backend technology you will find various ways of doing this.

You can also do this in javascript but you'll be more limited and it might lead to issues crossbrowser:

if((document.form1.upload.value.lastIndexOf(".jpg")==-1) {
   alert("Please upload only .jpg extention file");
   return false;


If you use jquery, checkout this question: http://stackoverflow.com/questions/651700/how-to-have-jquery-restrict-file-types-on-upload

You can also use the JQuery Validation plugin.

  rules: {
    field: {
      required: true,
      accept: "xls|csv"


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download