Javascript Reference - HTML DOM Input Datetime required Property








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

Browser Support

required No No No No No

Syntax

Return the required property.

var v = datetimeObject.required 

Set the required property.

datetimeObject.required=true|false

Property Values

Value Description
true|false Set whether a datetime field is a required part of form.
  • true - The datetime field is required
  • false - Default. The datetime field is not required




Return Value

A Boolean type value, true if the datetime field is a required part of form submission, otherwise false.

Example

The following code shows how to set a datetime field to be required.


<!DOCTYPE html>
<html>
<body>
<!-- www .j a  v a 2s  .c  om-->
<form action="url">
  Date: <input type="datetime" id="myDatetime" name="date">
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    document.getElementById("myDatetime").required = true;
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get if a datetime field must be filled out before submitting a form.


<!DOCTYPE html>
<html>
<body>
<!-- w  w  w  .ja  va 2s. c  o m-->
<form action="url">
  Date: <input type="datetime" id="myDatetime" name="date" required>
  <input type="submit">
</form>
<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 code above is rendered as follows: