HTML | DOM Input FileUpload required Property

The Input FileUpload required Property is used to set or return whether a file in the file upload field must be selected/uploaded before submitting a form.
This property reflects the HTML required attribute.

Syntax:

  • Return the required property:
    fileuploadObject.required
  • Set the required property:
    fileuploadObject.required=true|false

Property Values:
true: Return ‘true’ when the file upload field is required.
false: Return ‘false’ when the file upload field is not required.

Return Value: A Boolean value that displays the status of file upload required field.

Examples-1: Return FileUpload required Property.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
      Input FileUpload required Property
  </title>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1>
          Geeks for Geeks
      </h1>
        <form action="/action_page.php">
            <input type="file"
                   id="myFile"
                   required>
            <br>
            <input type="submit" 
                   value="Submit">
        </form>
  
        <p id="demo"></p>
  
        <button onclick="myFunction()">
          Try it
      </button>
  
        <script>
            function myFunction() {
                var x = 
                    document.getElementById(
                      "myFile").required;
                document.getElementById("demo").innerHTML = x;
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:
Before:

After:

Examples-2: Set FileUpload required Property

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
      Input FileUpload required Property
  </title>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1>
          Geeks for Geeks
      </h1>
        <form action="/action_page.php">
            <input type="file"
                   id="myFile">
            <br>
            <input type="submit" 
                   value="Submit">
        </form>
  
        <p id="demo"></p>
  
        <button onclick="myFunction()">
          Try it
      </button>
  
        <script>
            function myFunction() {
                var x =
                    document.getElementById(
                      "myFile").required = "true";
                
                document.getElementById("demo").innerHTML = x;
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:
Before:

After:

Supported Browsers:

  • Google Chrome
  • Mozilla Firefox
  • Edge 10.0
  • Opera
  • Apple Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.