jQuery validation for multiple file upload


Please find below code It’s allowed only png, jpg, jpeg and pdf files and upload upto 3 files and not more than 10MB
jQuery(‘input[name=”file[]”]’).change(function(){
var tot_size=0;
var count_files = jQuery(this).get(0).files.length;
for (var i = 0; i < jQuery(this).get(0).files.length; ++i) {
var file_size=jQuery(this).get(0).files[i].size;
tot_size = tot_size+file_size;
}
for (var i = 0; i < jQuery(this).get(0).files.length; ++i) {
var file1=jQuery(this).get(0).files[i].name;
if(!/(\.png|\.jpg|\.jpeg|\.pdf)$/i.test(file1))
{
jQuery(‘input[type=”submit”]’).attr(‘disabled’,’disabled’);
jQuery(“.file_error”).text(“”);
jQuery(this).css(“border”, “1px solid #c00”);
var err_var=true;
}else{
jQuery(“.file_error”).text(“Only png, jpg, jpeg and pdf files are allowed (Upload upto 3 files and 10MB).”);
jQuery(‘input[type=”submit”]’).removeAttr(‘disabled’,’disabled’);
jQuery(this).css(“border”, “1px solid #ddd”);
var err_var=false;
}
}
if(err_var==false){
if(tot_size < 10485760 && count_files < 4){
jQuery(“.file_error”).text(“Only png, jpg, jpeg and pdf files are allowed (Upload upto 3 files and 10MB).”);
jQuery(‘input[type=”submit”]’).removeAttr(‘disabled’,’disabled’);
jQuery(this).css(“border”, “1px solid #ddd”);
}else{
jQuery(‘input[type=”submit”]’).attr(‘disabled’,’disabled’);
jQuery(“.file_error”).text(“”);
jQuery(this).css(“border”, “1px solid #c00”);
}
}
});

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s