The HTML DOM Input Datetime required property determines whether Input Datetime is compulsory to set or not.
Syntax
Following is the syntax −
- Returning boolean value - true/false
inputDatetimeObject.required
- Setting required to booleanValue
inputDatetimeObject.required = booleanValue
Boolean Values
Here, “booleanValue” can be the following −
| booleanValue | Details |
|---|---|
| true | It defines that it is compulsory to set the datetime field to submit form. |
| false | It is the default value and to set datetime field is not compulsory. |
Example
Let us see an example of Input Datetime required property −
<!DOCTYPE html>
<html>
<head>
<title>Input Datetime required</title>
</head>
<body>
<form>
Final Exam Datetime: <input type="datetime" id="datetimeSelect" value="" required>
</form>
<button onclick="finalizeDatetime()">Confirm Datetime</button>
<div id="divDisplay"></div>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputDatetime = document.getElementById("datetimeSelect");
divDisplay.textContent = 'Exam Datetime Required: '+inputDatetime.required;
function finalizeDatetime() {
if(inputDatetime.value === '')
divDisplay.textContent += ', Give a valid Exam Datetime. ';
else
divDisplay.textContent = 'Final Exam Datetime: '+inputDatetime.value
}
</script>
</body>
</html>Output
This will produce the following output −
Clicking ‘Confirm Datetime’ button −

After clicking ‘Confirm Datetime’ button with value of datetime field set −
