file upload size limit validation in javascript

Today, We want to share with you file upload size limit validation in javascript.In this post we will show you File upload size validation using jQuery with demo, hear for multiple file upload size limit validation in javascript we will give you demo and example for implement.In this post, we will learn about How to check file input size with an example.

Validation of file size while uploading using JavaScript / jQuery

var uploadField = document.getElementById("file");

uploadField.onchange = function() {
    if(this.files[0].size > 307200){
       alert("File is too big!");
       this.value = "";
    };
};

jquery get file size in kb

In this example if we upload File which size is greater than 500kb it give a message.
Example 1: index.html












File size validation before upload

Example 2:

var maxSizeLimit = '1024';
function fileSizeValidate(profilecontent) {
	 if (profilecontent.files && profilecontent.files[0]) {
                var fsize = profilecontent.files[0].size/1024;
                if(fsize > maxSizeLimit) {
                	 alert('Maximum file size exceed, This file size is: ' + fsize + "KB");
                	 return false;
                } else {
                	return true;
                }
     }
 }

Image extension validation before upload

Example 3:

var validExt = ".png, .gif, .jpeg, .jpg";
function fileExtValidate(profilecontent) {
 var filePath = profilecontent.value;
 var getFileExt = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
 var check = validExt.indexOf(getFileExt);
 if(check < 0) {
 	alert("Sorry, This file is not allowed, please upload valid file.");
 	return false;
  } else {
  	return true;
  }
}

I hope you get an idea about Get File Size before Uploading in JavaScript and jQuery.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment