Javascript DOM HTML Input Date required Property get

Introduction

Find out if a date field must be filled out before submitting a form:

var x = document.getElementById("myDate").required;

Click button to find out if the date field must be filled out before submitting the form.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Date: <input type="date" id="myDate" name="date" required>
  <input type="submit">
</form>/* w w  w .j  ava  2 s . c o m*/
<button onclick="myFunction()">Test</button>
<p id="demo"></p>

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

</body>
</html>

The required property sets or gets whether a date field must be filled out before submitting a form.

This property mirrors the HTML required attribute.

The required property accepts and returns a boolean type value.

Value Description
trueThe date field is a required part of form submission
false Default. The date field is not a required part of form submission

It returns true if the date field is a required part of form submission, otherwise it returns false.




PreviousNext

Related