In this tip, I explain validation with a file upload control using regular expression validation control in ASP.NET. This tip 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:
Collapse | Copy Code
ValidationExpression=/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(<a href="file://\\(\w[\w">\\(\w[\w</a>].*))+
(.avi|.AVI|.WMV|.wmv|.wav|.WAV|.mpg|.MPG|.mid|.MID|.asf|.ASF|.mpeg|.MPEG)$/
Code
The code for the
.aspx page is:
Collapse | Copy Code
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
<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"
ValidationExpression="/^(([a-zA-Z]:)|(\\{2}\w+)\$?)
(\\(\w[\w].*))+(.avi|.AVI|.WMV|.wmv|.wav|.WAV|.mpg|.MPG|.mid|
.MID|.asf|.ASF|.mpeg|.MPEG)$/"
ValidationGroup="PartnerProfileUpdate"
SetFocusOnError="true"></asp:RegularExpressionValidator>
<br />
<asp:Button ID="Button1" runat="server"
Text="Save" OnClick="Button1_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</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
The regular expression to validate audio file formats is:
Collapse | Copy Code
ValidationExpression=/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$/
Output
The regular expression to validate documents and text file formats is:
Collapse | Copy Code
ValidationExpression=/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX|.txt|.TXT)$/
Output
The regular expression to validate image file formats is:
Collapse | Copy Code
ValidationExpression=/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+
(.jpeg|.JPEG|.gif|.GIF|.png|.PNG|.JPG|.jpg|.bitmap|.BITMAP)$/
Output