Input FileUpload required Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input FileUpload

Description

The required property sets or gets whether a file in the file upload field must be selected before submitting a form.

This property reflects the HTML required attribute.

Set the required property with the following Values

Value Description
true|false Sets whether a file upload field should be a required part of form submission.
  • 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

Return Value

A Boolean, returns true if the file upload field is a required part of form submission, otherwise it returns false

The following code shows how to check if a file must be selected before submitting a form:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form action="#">
  Select a file: <input type="file" id="myFile" name="usr_file">
  <input type="submit">
</form>/*from  w w w  .j  av  a  2s.  c  om*/

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var v = document.getElementById("myFile").required;
    document.getElementById("demo").innerHTML = v;
}
</script>

</body>
</html>

Related Tutorials