Search code examples
javascripthtmlasp.netvalidationhtml-input

validator html input type file


How can we validate the html <input type="file" /> from asp.net. I have tried <asp:fileUpload />it is working fine and I want to validate the html input. I have tried this piece of code :

  <input id="AttachmentFileUpload" type="file" size="45" name="LogoUploadedToUpload"
  class="imguploader" onchange="return validateFormToUpload();" />
  <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ValidationGroup="SendMailValidationGroup"
   ControlToValidate="AttachmentFileUpload" ErrorMessage="Attachment Is Required." CssClass="errorMessage"></asp:RequiredFieldValidator>
   CssClass="errorMessage"></asp:Label>

and I have tried this and in this context it works fine

    <asp:FileUpload ID="AttachmentFileUpload" runat="server"></asp:FileUpload>
    <asp:Button runat="server" ID="UploadButton" Text="Upload" OnClientClick="return validateFormToUpload();" />

But my problem arises here when i use asp:fileuploader the page shows error and when i use html input it works fine and I have decided to use html control. Now the problem is How can I validate html file <input type="file"/>? Any solution are surely appretiated and I have googled for the solution but I didn't get any answer.


Solution

  • You can use below code

    HTML

    <input id="fileSelect" type="file" >
    

    jQuery code

    $('#fileSelect').change(function(){   
        if($(this).val() == '')
            alert('no file');
        else
            alert('file');
    });
    

    Fiddle Demo