简体   繁体   中英

limit number file selections in a file input

Is there a way to limit the number of files you can select using the input file plus the multiple attribute.

<input type="file" accept="image/*" multiple/>

I am aware of one approach that is to check the length of files after selection using the onChange event. While this works I would like to prevent user from selecting more than 5 image files in the file select dialog phase.

Is this possible?

Unfortunately there is no way if limiting the input field as specified in W3C. At least not without Javascript. The following would work but is probably not as nice as you would have liked:

<input type="file" onchange="checkFiles(this.files)">

function checkFiles(files) {       
    if(files.length>5) {
        alert("length exceeded");
        files.slice(0,5);
        return false;
    }       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM