title: validate asp fileupload control client side
body: <p>i have a asp fileupload control on my page. how to validate the selected file on client side.
  validation rules: 
  1. file must be jpeg,png,bmp or gif.
  2. file size must be within 25 kb and 2 mb.
  please help.
  thanks.<br>
  I Have Tried The Following Code to validate the extension </p>
  
  <pre><code>&lt;asp:FileUpload ID="FileUpload2" runat="server"/&gt;                    
  &lt;asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ValidateFileUpload" ErrorMessage="Invalid file type. Only .gif, .jpg, .png, .bmp and .jpeg are allowed." ControlToValidate="FileUpload2" ValidationGroup="update"&gt;&amp;nbsp;&lt;/asp:CustomValidator&gt;
  &lt;script language="javascript" type="text/javascript"&gt;
      function ValidateFileUpload(Source, args) {
          var fuData = document.getElementById('&lt;%= fuData.ClientID %&gt;');
          var FileUploadPath = fuData.value;
  
          if (FileUploadPath == '') {
              // There is no file selected
              args.IsValid = false;
          }
          else {
              var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
  
              if (Extension == "jpg" || Extension == "jpeg" || Extension == "png" || Extension == "gif" || Extension == "bmp") {
                  args.IsValid = true; // Valid file type
              }
              else {
                  args.IsValid = false; // Not valid file type
              }
          }
      }
  &lt;/script&gt;
  </code></pre>