Friday 29 March 2019

Check empty file using jquery

When you use file control from the input tag then for validation purpose you need to check the file name. if no file is selected then an error message should be displayed. This post will help you to achieve the same. When a user doesn’t select a file and an attempt is made to read file name then JS engines throw an exception which must be caught otherwise you will not be able to read and validate the empty file.

<input type="file" class="upload" onchange="readURL(this)" id="fileRO" accept="image/x-png,image/jpeg" />

  try 
  {
        alert($j("#fileRO")[0].files[0].name);
  } 
  catch (exception) 
  {
        alert('Please select a image file');

  }

No comments:

Post a Comment