ItSolutionStuff.com

JQuery Validate File Size with Multiple File Upload Example

By Hardik Savani • June 6, 2023
jQuery

Hey Dev,

This tutorial will provide an example of jquery multiple image upload with size validation. This tutorial will give you a simple example of multiple file upload with max size validation. you will learn jquery multiple select file validate file size before upload. you can understand a concept of jquery multiple image upload with size validation. Let's get started with multiple file upload with max size validation.

Sometimes we require to add validation of max file size using jquery, If we have only single file for validation then we can do it easily that, but if we have multiple file then you have to calculate size of all selected files and then check max required file size.

So, in this example you can see how to check validation for max size in multiple file select using jquery.

Example:

<html lang="en">

<head>

<title>Jquery - multiple image upload with size validation</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>

</head>

<body>


<div class="container text-center">


<div class="grid-stack">

<input type="file" id="fUpload" multiple />

<button>Save</button>

</div>


<script type="text/javascript">

$(document).ready(function(){

$('#fUpload').change(function(){

var fp = $("#fUpload");

var lg = fp[0].files.length;

var items = fp[0].files;

var fileSize = 0;

if (lg > 0) {

for (var i = 0; i < lg; i++) {

fileSize = fileSize+items[i].size;

}

if(fileSize > 2097152) {

alert('File size must not be more than 2 MB');

$('#fUpload').val('');

}

}

});

});

</script>


</div>


</body>

</html>

I hope it can help you...

Tags: jQuery
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Calculate Number of Days Between Two Dates in JQuery

Read Now →

How to Get Unique Values from Array in JQuery?

Read Now →

JQuery Disable Submit Button on Click to Prevent Multiple Form Submits

Read Now →

How to Only Allow Numbers in a Text Box using jQuery?

Read Now →

How to Remove Duplicate Value from Array in JQuery?

Read Now →

How to Change Date Format in JQuery?

Read Now →

How to Add Custom Validation for Image File Upload in Summernote?

Read Now →

How to Call AngularJS Controller Function in JQuery?

Read Now →

JQuery Add Remove Input Fields Dynamically Example

Read Now →

JQuery Accordion using JQuery UI Example

Read Now →

How to Disable Right Click Menu using JQuery?

Read Now →

JQuery Tooltip Example using JQuery UI Plugin

Read Now →

JQuery Hide and Show Password using Eye Icon Example

Read Now →