Find out if a file must be selected/uploaded before submitting a form:
var x = document.getElementById("myFile").required;
Click button to find out if a file in the file upload field is required.
<!DOCTYPE html> <html> <body> <form action="/action_page.php"> Select a file: <input type="file" id="myFile" name="usr_file" required> <input type="submit"> </form>//from ww w. j a va2 s . c om <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myFile").required; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The required
property sets or gets whether a file in the file upload field is required.
This property mirrors the HTML required attribute.
The required
property accepts and returns a boolean value.
Value | Description |
---|---|
true | The file upload field is a required part of form submission |
false | Default. The file upload field is not a required part of form submission |
The required
property returns true if the file upload field is a required part of form submission, otherwise it returns false.