I am new to jquery/javascript now i have a form in which user can upload multiple files which is working just fine but i need to do alterations to it this is the working code.
$('#userFiles').change(function() {
var combinedSize = 0;
for (var i = 0; i < this.files.length; i++) {
combinedSize += (this.files[i].size || this.files[i].fileSize);
}
if (combinedSize > 104857600) {
toastr["error"]("Max upload size is 100MB");
}
});
HTML
<input type="file" class="form-control" id="userFiles" name="userFiles[]" multiple/>
Now what i want to do is before submitting the check whther the collective files sizes are more than 100 if yes then stop the form from submitting and display an error that you cannot upload more than 100MB now this is the updated code.
$("form").on("submit", function(e) {
e.preventDefault();
$('#userFiles').change(function() {
var combinedSize = 0;
for (var i = 0; i < this.files.length; i++) {
combinedSize += (this.files[i].size || this.files[i].fileSize);
}
if (combinedSize > 104857600) {
toastr["error"]("Max upload size is 100MB");
}
});
});
My issue is that it using this function because it is an event for input change when it comes to form submit i cannot use another event inside it i want the logic to work only on form submit how can i replace this with the userFile id attribute? i tried couple of ways but it is not working.