Friday 15 April 2016

Validate File Extension and Size Before Upload


image-file-formats.jpg
Here is the example to validate the file extension and size before upload. Regular expression has been used to validate the file extension. And to validate file size, we use size property of file input i.e., img.files[0].size. Onclick event of button, validate() function is being called. First, it checks for file extensions (jpg, jpeg, png, etc) and give error message for the files having other extensions. Then, it validates the file size by calculating the file size using img.files[0].size.
Example:
  1. <html>
  2. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  3. <script type="text/javascript">
  4. function Validate()
  5. {
  6.    var image =document.getElementById("image").value;
  7.    if(image!='')
  8.    {

Read full blog at our highly specific C, Java, PHP, Java, iPhone, Javascript Questions, about the topic described above "Validate File Extension and size Before Upload". You can also learn much more about different programming technologies and can enhance your tech skills.

No comments:

Post a Comment