HTML DOM Input Datetime required Property

HTMLWeb DevelopmentFront End Technology

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 −

booleanValueDetails
trueIt defines that it is compulsory to set the datetime field to submit form.
falseIt is the default value and to set datetime field is not compulsory.

Example

Let us see an example of Input Datetime required property −

 Live Demo

<!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 −

raja
Published on 23-Jul-2019 18:00:35
Advertisements