Client side validation of File type before you upload a file

Posted: June 18, 2008 in ASP.NET
Tags: , , ,

To validate a file type or say file extension on client side before you upload a file can be performed by just putting a Regular Expression Validator. Here, i have provided one example to allow uploading only .jpg or .gif files. All you need is a FileUpload control and a Regular Expression Validator to check file type.

<asp:FileUpload ID=”FileUpload1″ runat=”server” />

<asp:RegularExpressionValidator ID=”RegularExpressionValidator1″ runat=”server” ControlToValidate=”FileUpload1″ Display=”Dynamic” ErrorMessage=”Upload a valid file” ValidationExpression=”^.+(.jpg|.JPG|.gif|.GIF)$”></asp:RegularExpressionValidator>

If you want to put your own file type validation then just replace “.jpg,.JPG,.gif,.GIF” text, and you can add more file types by adding “|” (pipe) sign and your own file type to validate.

Happy Coding!!

Advertisements
Comments
  1. Ameet says:

    Great yaar, its working for all type of files

    Thanks & Regards
    Ameet

  2. Neha Bhatt says:

    File dialoge pop up for Browse button of asp upload control , works only with mouse click or space click on it.
    How to make its behaviour just like button where we can call button_Click function on pressing key enter on it

  3. hspinfo says:

    Hi Neha,

    I think you can not change the default behavior of the fileupload control, unless you make your own usercontrol.

    However, if you test the fileupload control in a Firefox, it will work same as you are willing to…

    You can also check the this link, if you want to perform the similar kind of operation…

  4. hi says:

    hi,client side file type valdation using regular expression is working perfectly in IE.but i check it in FF 3.0 and it’s not working.I try to find the solution for that.
    thanks

  5. debasish says:

    hi
    this is good working fine.
    no need script in javascript

  6. Ubaid says:

    Great! It saved a lot of time 🙂

  7. Taj says:

    Response to hi on 9/26/08

    It works in firefox.
    Make sure your hosting server or hosting company’s server is running asp.net 2.0 or higher. If the hosting server is running asp.net 1.x, the expression will only work in Internet Explorer.

  8. Shamira says:

    This solution is great, but doesn’t work in FF. I’m using this RegEx to validate, but works only in IE.
    ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+((.jpg)|(.gif)|(.png)|(.JPG)|(.GIF)|(.PNG)|(.bmp)|(.BMP)|(.jpeg)|(.JPEG))$

    Am I doing something wrong here? I’m using IIS6/7 with .Net 2.0.

  9. jatin says:

    good job!!!

  10. Ritesh says:

    Thx yaar……

    gr8 work

    It works for all types of file

    Thanks & Regards,
    Ritesh

  11. Amit says:

    Thx…. It’s GOOD!!!!!

  12. Thanks.. Works fine (Y)

  13. Nice article for Javascripts professional. We will refer to students.
    PINNA INFOTECH

  14. Dattatraya Kale says:

    Great Job :)….
    It is working in IE, FF.
    Thanks for the better solution.

  15. Hamdy says:

    Great Job thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s