Javascript Form How to - Access form object that contains the File Upload Input box








Question

We would like to know how to access form object that contains the File Upload Input box.

Answer


 <!--from  w  w w . jav a2s.co  m-->
<html>
<body>
    <script language="JavaScript">
    function checkFiles(){
         if (document.secret.file1.value == ""){
             console.log("You did not enter anything for file 1");
         }
         if (document.secret.file2.value == ""){
             console.log("You did not enter anything for file 2");
         }
         else {
              console.log("The files are okay and will be uploaded");
         }
    }
    </script>
    <form name="secret">
    Please choose two files to upload.
    <br><br>
    File 1:<input type="file" name="file1">
    <br><br>
    File 2:<input type="file" name="file2">
    <br><br>
    <input type="button" value="Verify" onClick='checkFiles()'>
    </form>
</body>
</html>

The code above is rendered as follows: