I am using in my web page to upload files (using
ajaxupload
). It will allow user to upload multip
<input id="files" type="file" name="files[]" multiple="multiple" onchange="checkFiles(this.files)">
function checkFiles(files) {
if(files.length>10) {
alert("length exceeded; files have been truncated");
let list = new DataTransfer;
for(let i=0; i<10; i++)
list.items.add(files[i])
document.getElementById('files').files = list.files
}
}
This function will not allow to select files more than 10.
this piece of code does what you desire and also stops the form from being submitted.
<script>
$(function() {
$("button[type = 'submit']").click(function(event) {
var $fileUpload = $("input[type='file']");
if (parseInt($fileUpload.get(0).files.length) > 10) {
alert("You are only allowed to upload a maximum of 10 files");
event.preventDefault();
}
});
});
</script>