How to limit the maximum files chosen when using multiple file input

file, html, javascript

Solution

You could run some jQuery client-side validation to check:

$(function(){
    $("input[type='submit']").click(function(){
        var $fileUpload = $("input[type='file']");
        if (parseInt($fileUpload.get(0).files.length)>2){
         alert("You can only upload a maximum of 2 files");
        }
    });    
});​

http://jsfiddle.net/Curt/u4NuH/

But remember to check on the server side too as client-side validation can be bypassed quite easily.

Problem

When using `<input type="file" multiple>` it's possible for the user to choose multiple files. How does ones sets a limit for how many files can be chosen, for instance two?

Original source

Related problems