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 asp.net.
Description:
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.
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="http://www.w3.org/1999/xhtml">
<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;
}
else
{
//If the condition not satisfied shows error message.
alert("Only .doc, docx files are allowed!");
return false;
}
}
} //End of function validate.
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
<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>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
Demo
Other Related validation posts
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
22 comments :
Can you please explain more how to link to the server side etc.? Just need more complete instructions.
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.
sir,
Do you have any project related to survey.
OR How can we strore survey result in database.
when i used jquery and ajax....
it is not working..
any solutions. ??
not working in firefox
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 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
hi can you give me some code where i have only characters and only one special character in my text box please!!!!
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)$/;
Hi
I want to upload .pdf files only and open those files in new tab
not to be download
mozilla firefox not supported
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
Sir,could u please help me how to display image in image control after selecting file using FileUpload i my page???
sir can u please tell me that how to check validation for images and if image format is correct then it must upload it
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 upload..so i want to validate a file irrespective of its extension
espero les ayude amigos!
var extensiones=/\.(gif|jpg|jpeg|png|PNG|JPEG|JPEG|GIF)$/i;
var miarchivo="demo.jpg";
if(extensiones.test(miarchivo)){
alert("true");
}else{
alert("false");
}
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
Where to place the 'function' in a child form which is connected to a master? There is no heading tag in child form.!!
this is working in IE only javascript is not working in Mozilla
this is working in IE only javascript is not working
this is working in IE only javascript is not working in Mozilla
Hi
I want to upload .pdf files only and open those files in new tab
not to be download
Note: Only a member of this blog may post a comment.