Javascript DOM HTML Input Datetime required Property get

Introduction

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

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

Click button to find out if the datetime field is required for the form.

View in separate window

<!DOCTYPE html>
<html>
<body>

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

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

</body>
</html>

The required property sets or gets whether a datetime 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.

Property Values

Value Description
true The datetime field is a required part of form submission
false Default. The datetime field is not a required part of form submission

The required property returns true if the datetime field is a required part of form submission, otherwise it returns false.




PreviousNext

Related