File Upload Validation Using JQuery

If in HTML Form, you have a file input, you can validate the extensions as soon as the user upload a file using the JQuery Code given below :

Here’s the HTML Code :

<input id="fileupload" name="fileupload" type="file" />

Here’s the JQuery Code:

var fileTypes = ['jpg', 'jpeg', 'png', 'pdf']; //acceptable file types
function readURL(input) {
if (input.files && input.files[0]) {
var extension = input.files[0].name.split('.').pop().toLowerCase(), //file extension from input file
isSuccess = fileTypes.indexOf(extension) > -1; //is extension in acceptable types
if (isSuccess) {
var reader = new FileReader();
} else {
alert("File should be in jpg, png, jpeg or pdf format.")
return false;
$("#fileupload").change(function(){ //Dynamically called when a file is uploaded

When the user uploads a file, readURL function is called. In this function it will be checked if the extension of the uploaded file is in the permitted fileTypes array.


One thought on “File Upload Validation Using JQuery

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s