简体   繁体   中英

Multiple File upload validation using javascript/jQuery

just trying to do the basic validation for multiple file uploads. it only validates the first file. i can't find where I did the mistake. Help me out with validating the form.

Have updated the actual scenerio of the code.

HTML

<form id="img_multiple">
   <div id="append">
      <input type="file" name="img[]" id="img" class="img" />
   </div>
   <button class="add_img">Add</button> 
   <input type="submit" />
</form>

JS

$(document).on('click', '.add_img', function(){
   var html = '';
   var html = '<input type="file" name="img[]" id="img" class="img" />';
   $('#append').append(html);
});

$(document).on('submit', '#img_multiple', function(e){
   e.preventDefault();
   if($(".img").val() != '')
   {
   var count_of = $(".img").get(0).files.length;
   alert(count_of);
   for (var i = 0; i < $(".img").get(0).files.length; ++i)
   {
       var img =$(".img").get(0).files[i].name;
       var img_file_size=$(".img").get(0).files[i].size;
       if(img_file_size<10485760)
       {
          var img_ext = img.split('.').pop().toLowerCase();
          if($.inArray(img_ext,['jpg','jpeg','pdf','png'])===-1)
          {
             od_error = 'Yes';
             $('#img_err').html("<span class='text-danger'>File ("+img+") type not allowed.</span>");
          }
          else
          {
             $('#img_err').html("");
          }
       }
       else
       {
          od_error = 'Yes';
          $('#img_err').html("<span class='text-danger'>File("+img+") size is too big.</span>");
       }    
    }  
 }
 else
 {
   od_error = 'Yes';
   $('#img_err').html("<span class='text-danger'>Upload documents</span>");
 }
});

Instead of id='img' try class='img' and instead of #img use .img . The error you are getting is because id can be used in only one element but in this case you have used same id for 3 elements.

You can also use the attribute accept="image/*"

EDIT: Instead of your

$(document).on('submit', '#img_multiple', function(e)

Try this code instead

$('.img').on('change', function(){
   if($(this).val() != '')
   {
   var count_of = $(this).get(0).files.length;
   alert(count_of);
   for (var i = 0; i < $(this).get(0).files.length; ++i)
   {
       var img =$(this).get(0).files[i].name;
       var img_file_size=$(this).get(0).files[i].size;
       if(img_file_size<10485760)
       {
          var img_ext = img.split('.').pop().toLowerCase();
          if($.inArray(img_ext,['jpg','jpeg','pdf','png'])===-1)
          {
             od_error = 'Yes';
             $('#img_err').html("<span class='text-danger'>File ("+img+") type not allowed.</span>");
          }
          else
          {
             $('#img_err').html("");
          }
       }
       else
       {
          od_error = 'Yes';
          $('#img_err').html("<span class='text-danger'>File("+img+") size is too big.</span>");
       }    
    }  
 }
 else
 {
   od_error = 'Yes';
   $('#img_err').html("<span class='text-danger'>Upload documents</span>");
 }
});

Also edit your html code where you have placed the button as

<button type='button' class="add_img">Add</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM