How to Validate file type in FileUpload Control in ASP.NET

Leave a Comment
In this tutorial, how to validate File Upload Control to upload only images using Regular Expressing Validation Control in Asp.Net.

First add File Upload Control to Asp.Net web page.

<asp:FileUpload ID="fileuploaduserphoto" runat="server" />
Now, add following Regular Expression Validation control to Asp.Net Webpage.
 <asp:RegularExpressionValidator runat="server" ID="valUpTest" ControlToValidate="fileuploaduserphoto" ValidationGroup="up"
            ErrorMessage="Image Files Only (.jpg, .png, .gif)"
            ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF|.jpeg|.JPEG|.png|.PNG)$" />

The complete source code
<asp:FileUpload ID="fileuploaduserphoto" runat="server" />
        <asp:RegularExpressionValidator ForeColor="Red" runat="server" ID="valUpTest" Display="Dynamic"
            ControlToValidate="fileuploaduserphoto" ValidationGroup="up" ErrorMessage="Image Files Only (.jpg, .png, .gif)"
            ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF|.jpeg|.JPEG|.png|.PNG)$" /><br />
        <asp:Button ValidationGroup="up" ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />

Demo
Validate file type in FileUpload Control in ASP.NET

0 comments:

Post a Comment