Validation For File Upload Control in ASP.NET


In this article I explain validation with a file upload control using regular expression validation control in ASP.NET. This article will help you to validate the extension of a file to be uploaded before the file is uploaded in ASP. NET.


Usually for checking the desired file extensions in a file upload control we use C# code to determine whether the file extension is valid or not.

But, we can also validate the file type of the file upload control using the ASP.Net regular expression validation control. We need to only provide the correct regular expression for the desired file type to the validation control. It will automatically validate the extension of the uploaded file before the page is posted back.

Here I will show you regular expressions for various file formats to validate a file upload.

Regular Expression to validate video file formats

Here my need is to validate a file for only video formats, so that the user is able to upload the file in the valid video formats.

For this purpose I create an ASP.Net application that contains a file upload control and with a regular expression validation control. To validate only the video file, I provide the regular expression for video file formats to the validation control and some properties of the validation control to validate the file upload control.

The expression for video formats is:



The code for the .aspx page is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head id="Head1" runat="server">




    <form id="form1" runat="server">


        <table align="center">



                    <b>Validation to File Upload Control</b>

                    <br />

                    <asp:FileUpload ID="fileUploadVideo" runat="server" />          

                    <asp:Label ID="labelVideomsg" runat="server" ForeColor="Red" Visible="false"

                    Style="margin-left: -38px;"></asp:Label>

                    <asp:RegularExpressionValidator ID="RegularExpressionValidator7"

                     runat="server" ControlToValidate="fileUploadVideo"

                     ErrorMessage="Only .avi, .mpg, .wav, .mid, .wmv, .asf and .mpeg Video formats are allowed." ForeColor="Red"


                     ValidationGroup="PartnerProfileUpdate" SetFocusOnError="true"></asp:RegularExpressionValidator>

                    <br />

                    <asp:Button ID="Button1" runat="server" Text="Save"  OnClick="Button1_Click" />                 








Now, run the application and see the output.

When you upload an invalid file it will immediately give the validation error message indicating an invalid file format.

The regular expression to validate audio file formats is:



The regular expression to validate documents and text file formats is:



validation for upload control

The regular expression to validate image file formats is:



Up Next
    Ebook Download
    View all
    View all