Click here to Skip to main content
15,606,012 members
Articles / Web Development / ASP.NET
Posted 16 Jul 2013


10 bookmarked

Upload a file with required validation using jQuery

Rate me:
Please Sign up or sign in to vote.
4.79/5 (7 votes)
16 Jul 2013CPOL1 min read
Uploading a File with the required validation


In this example, I will explain how to specify what file can be selected for uploading from a FileUpload control and until the valid file is selected the Upload button would be disabled. The complete validation process would be done with jQuery. And we will also see how to display a label instead of an alert message box.


In many forums, I see the question of how to validate the required file using JavaScript or jQuery.

Image 1

When you click on the Browse button, a window will be opened in the client side.

Image 2

Select any file and click the Open button. If you selected only .Jpg or .JPEG files, the Upload button would be enabled. Try out with both, like select a .jpg file and check if you can see the Upload button. If you click any other file than a .jpg extension file, then you would see a message saying that “Only '.jpeg','.jpg' formats are allowed”.

Selecting a non .jpg extension:

Image 3

Selecting a .jpg file:

Image 4

Using the code

In the file extension we have taken only .jpg or .jpeg files and we are checking weather the selected file from fileupload is a .jpg or not? If it is not a .jpg or .jpeg then we are displaying a label message Only .jpg format is allowed. If it's a .jpg file then we are enabling the upload button.

<script src="//"></script> 
<script type="text/javascript">
    $(function () {
        $('#<%=fuPhoto.ClientID %>').change(
            function () {
                var fileExtension = ['jpeg', 'jpg'];
                if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
                    // alert("Only '.jpeg','.jpg' formats are allowed.");
                    $('#<%=btnUpload.ClientID %>').attr("disabled", true);
                    $('#<%= myLabel.ClientID %>').html("Only '.jpeg','.jpg' formats are allowed.");
                else {
                    $('#<%=btnUpload.ClientID %>').attr("disabled", false);
                    $('#<%= myLabel.ClientID %>').html(" ");
<form id="form1" runat="server">
    <asp:FileUpload ID="fuPhoto" runat="server" />
    <asp:Button ID="btnUpload" Text="Upload" runat="server" Enabled="false" />
    <asp:Label ID="myLabel" runat="server" ForeColor="#CC0000" />

In the file extension, you can write your own extensions as per your requirements, like .gif, .doc, .ppt etc.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
India India
I am a 29 year old software Web Developer from Hyderabad, India. I have been working since approximately age 25. Where as in IT Development industry since 27. I am Microsoft Certified Technology Specialist.

I have taught myself in development, beginning with Microsoft's technologies ASP.NET, Approximately 3 years ago, I was given an opportunity to work as a freelance in the tech field. Now I am working as a web developer where my roles make me purely in web based technology solutions which manage and control access to applications and patient information stored in legacy systems, client-server applications.

I too had an opportunity to train some IT professionals with technical skills in development area. Which became my passion.

I have worked on various .NET framework versions(2.0 , 3.5, 4.0) and have been learning every new technology being introduced. Currently, I am looking forward to working in R & D in .Net to create distributed, reusable applications.

Comments and Discussions

SuggestionUpload a File with the required validation and Regular expression validator Pin
Mukund Thakker3-Aug-13 2:36
professionalMukund Thakker3-Aug-13 2:36 
QuestionIs there a way to other file attributes? Pin
BobbyPhillips17-Jul-13 12:05
BobbyPhillips17-Jul-13 12:05 
AnswerRe: Is there a way to other file attributes? Pin
Mannava Siva Aditya17-Jul-13 18:59
Mannava Siva Aditya17-Jul-13 18:59 
GeneralRe: Is there a way to other file attributes? Pin
BobbyPhillips18-Jul-13 10:55
BobbyPhillips18-Jul-13 10:55 
GeneralRe: Is there a way to other file attributes? Pin
Mannava Siva Aditya18-Jul-13 19:01
Mannava Siva Aditya18-Jul-13 19:01 
GeneralRe: Is there a way to other file attributes? Pin
Shaik Akram31-Mar-14 0:24
professionalShaik Akram31-Mar-14 0:24 
<input type="file" data-val="true" data-val-required="please select a file" name="file"/>


try out this one just for validating fileupload control for checking only whether the user has selected any file or not.
GeneralMy vote of 5 Pin
Anurag Gandhi17-Jul-13 4:57
professionalAnurag Gandhi17-Jul-13 4:57 
GeneralRe: My vote of 5 Pin
Mannava Siva Aditya17-Jul-13 5:11
Mannava Siva Aditya17-Jul-13 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.