Friday 22 April 2016

file size validation in jquery


Introduction:

Here I will explain how to validate file size using Jquery or Javascript. Sometimes required  validation like File size cannot be more than 2Mb or 5MB otherwise alert to user "File size cannot be more than 2Mb or 5MB"

Description: 

In previously post I explained to  
file type validation in jquery
Change the column name or datatype or size in sql server
How to set or get textbox value using Javascript or Jquery
Server does not support secure connections  
textbox allow only characters javascript
button click event not firing in asp.net c#

And now here I explain to how to validate file size using Jquery or Javascript in asp.net.

Now copy the following javascript code in your page.


function IsFileValid() {

        var objFileUpload = document.getElementById("fuContentImage");
        if (objFileUpload.value != null || objFileUpload.value != "") {
        var file;
        file = objFileUpload.files[0];
      

 if (file.size > 2097152) //file.size returns Bytes of the file
        {
                alert("File size cannot be more than 2Mb.");
                return false;
        }
 
    }
}


And copy this html in your page


<h1>how to validate File size cannot be more than 2Mb or 5MB using Jquery or Javascript</h1>

<input type="file" name="Image" id="fuContentImage" />  

<input type="submit" onclick="return IsFileValid();" value="Add" class="btn btn-success" style="width:200px" />

No comments:

Post a Comment



Asp.net tutorials