How to limit maximum items on multiple input ()?


To allow multiple file uploads in HTML forms, use the multiple attributes. The multiple attributes work with email and file input types.


For limiting maximum items on multiple inputs, use JavaScript. Through this, limit the number of files to be uploaded. For example, let’s say only 2 files to be uploaded at once.

You can try to run the following code to learn how to use multiple attributes in HTML. With that, we will limit the number of files uploaded using JavaScript.

Example

Live Demo

<!DOCTYPE html>
<html>
   <head>
      <title>HTML file upload</title>
   </head>
   <body>
      <form>
         <input type="file"  action="/action_page.php"  name="name" multiple><br/>
         Upload multiple files, and click Submit.<br>
         <input type = "submit" value = "submit">
      </form>
      <script>
         $(function(){
            $("input[type = 'submit']").click(function(){
               var $fileUpload = $("input[type='file']");
               if (parseInt($fileUpload.get(0).files.length) > 3){
                  alert("You are only allowed to upload a maximum of 3 files");
               }
            });
         });
      </script>
   </body>
</html>

Updated on: 25-Feb-2020

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements