aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Regular expressions to validate file extension before file upload in javascript using

Dec 14, 2010

Here I will explain how to check or validate file type in file upload control using JavaScript and regular expression to check file extension before file upload using JavaScript in


I have one page that contains one file upload control to accept files from user and saving it in one folder. Here I need to give permission for user to upload only .doc or .docx files for that I have written validation by using JavaScript to validate files before save it in database.

Here I have some of regular expressions to validate file upload. 

Regular expression to validate file formats for .mp3 or .MP3 or .mpeg or .MPEG or m3u or M3U

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

Regular expression to validate file formats for .doc or .docx

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

Regular expression to validate file formats for .txt or .TXT

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.txt|.TXT)$/;

Regular expression to validate file formats for .jpeg or .JPEG or .gif or .GIF or .png or .PNG

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

Here my requirement is to validate file for only .doc or .docx for that I have written following code in aspx page

<html xmlns="">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function validate() {
var uploadcontrol = document.getElementById('<%=FileUpload1.ClientID%>').value;
//Regular Expression for fileupload control.
var reg = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX)$/;
if (uploadcontrol.length > 0)
//Checks with the control value.
if (reg.test(uploadcontrol))
return true;
//If the condition not satisfied shows error message.
alert("Only .doc, docx files are allowed!");
return false;
} //End of function validate.
<form id="form1" runat="server">
<table align="center">
<b>Restrict File Upload sample</b> <br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validate();" />
<br />
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>

Other Related validation posts

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


John said...

Can you please explain more how to link to the server side etc.? Just need more complete instructions.

Suresh Dasari said...

hi john,
here i am validating file before upload in clientside this is enough no need to link in service it will work for you based on your requirement.

Mohammad Raza said...

Do you have any project related to survey.
OR How can we strore survey result in database.

Er. Maharshi Pandya said...

when i used jquery and ajax....
it is not working..

any solutions. ??

Anonymous said...

not working in firefox

suresh gupta said...

Hiiii this is working on clientside and accordingly shows error when we try to upload some other file which is not desired.But at that time how to relate this javascript code to backend .aspx.vb code so that image should not be saved in database.

Suresh Dasari said...

@Suresh Gupta...
In javaScript you must use return false as mentioned in above code then if condition fails it will show alter message and stop the execution of function

Anonymous said...

hi can you give me some code where i have only characters and only one special character in my text box please!!!!

Anonymous said...

Hi Sir,

I have used this expression for validating file extension in javascript .But the problem is like this works fine in IE and chrome but doesnt work in Firefox is there any mistake in my expression.
/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG|.jpg|.JPG|.bmp|.BMP)$/;

manju said...

I want to upload .pdf files only and open those files in new tab
not to be download

Anonymous said...

mozilla firefox not supported

Dendupous Tshering said...

Sir Suresh, i have copied same code as u have provided above,but it has no effect on "FileUpload1" to give validate format...could u please tell me the reason or problem behind

Dendup Wangdi said...

Sir,could u please help me how to display image in image control after selecting file using FileUpload i my page???

priyanka said...

sir can u please tell me that how to check validation for images and if image format is correct then it must upload it

Anonymous said...

if some has changed the extnsion of a file like .exe or .html to jpeg or doc or pdf and then uploaded means what is going to happen?it will i want to validate a file irrespective of its extension

Anonymous said...

espero les ayude amigos!

var extensiones=/\.(gif|jpg|jpeg|png|PNG|JPEG|JPEG|GIF)$/i;
var miarchivo="demo.jpg";

Sunil said...

Sir i m trying to load an image when log in is done to the master page, but i need to check the format of the image before it loads can u help me

Bhargav Dutta said...

Where to place the 'function' in a child form which is connected to a master? There is no heading tag in child form.!!

Give your Valuable Comments

Other Related Posts

© 2010-2012 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.