Validation For File Upload Control in ASP.NET

Introduction

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.

Description

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:

ValidationExpression=/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.avi|.AVI|.WMV|.wmv|.wav|.WAV|.mpg|.MPG|.mid|.MID|.asf|.ASF|.mpeg|.MPEG)$/

Code

The code for the .aspx page is:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.         <table align="center">  
  11.             <tr>  
  12.                 <td>  
  13.                     <b>Validation to File Upload Control</b>  
  14.                     <br />  
  15.                     <asp:FileUpload ID="fileUploadVideo" runat="server" />                    <asp:Label ID="labelVideomsg" runat="server" ForeColor="Red" Visible="false"  
  16.                     Style="margin-left: -38px;"></asp:Label>  
  17.                     <asp:RegularExpressionValidator ID="RegularExpressionValidator7"  
  18.                      runat="server" ControlToValidate="fileUploadVideo"  
  19.                      ErrorMessage="Only .avi, .mpg, .wav, .mid, .wmv, .asf and .mpeg Video formats are allowed." ForeColor="Red"  
  20.                      ValidationExpression="/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.avi|.AVI|.WMV|.wmv|.wav|.WAV|.mpg|.MPG|.mid|.MID|.asf|.ASF|.mpeg|.MPEG)$/"  
  21.                      ValidationGroup="PartnerProfileUpdate" SetFocusOnError="true"></asp:RegularExpressionValidator>  
  22.                     <br />  
  23.                     <asp:Button ID="Button1" runat="server" Text="Save"  OnClick="Button1_Click" />  
  24.                 </td>  
  25.             </tr>  
  26.         </table>  
  27.     </div>  
  28.     </form>  
  29. </body>  
  30. </html> 

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.

Output

file-upload-control-validation-in-asp.net.png
The regular expression to validate audio file formats is:
 

ValidationExpression=/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$/


Output

validation-for-upload-control-asp.net(1).png

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

ValidationExpression=/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX|.txt|.TXT)$/

Output

validation for upload control inasp.net.htm.png

The regular expression to validate image file formats is:
 

ValidationExpression=/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.gif|.GIF|.png|.PNG|.JPG|.jpg|.bitmap|.BITMAP)$/


Output

validation-for-upload-control-asp.net.png


Similar Articles