Friday, 13 December 2013

Validating file uploader in Javascript

function ValidateFile()
    {
      var validFilesTypes=["bmp","gif","png","jpg","jpeg","doc","xls"];
      var file = document.getElementById("<%=fileupload.ClientID%>");
      var path = file.value;
      var ext=path.substring(path.lastIndexOf(".")+1,path.length).toLowerCase();
      var isValidFile = false;
      for (var i=0; i<validFilesTypes.length; i++)
      {
        if (ext==validFilesTypes[i])
        {
            isValidFile=true;
            return true;
        }
      }
      if (!isValidFile)
      {
         alert('File Format is invalid.["bmp","gif","png","jpg","jpeg"] are allowed.');
         return false;
      }
    
     }   

No comments:

Post a Comment