HTML DOM Input DatetimeLocal required Property

HTMLWeb DevelopmentFront End Technology

The HTML DOM Input DatetimeLocal required property determines whether Input DatetimeLocal is compulsory to set or not.

Syntax

Following is the syntax −

  • Returning boolean value - true/false
inputDatetimeLocalObject.required
  • Setting required to booleanValue
inputDatetimeLocalObject.required = booleanValue

Boolean Values

Here, “booleanValue” can be the following −

booleanValueDetails
trueIt is compulsory to set the datetimeLocal field to submit form.
falseIt is the default value and to set datetimeLocal field is not compulsory.

Example

Let us see an example of Input DatetimeLocal required property −

<!DOCTYPE html>
<html>
<head>
<title>Input DatetimeLocal required</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>Datetime-Local-required</legend>
<label for="datetimeLocalSelect">Examination Slot :
<input type="datetime-local" id="datetimeLocalSelect" value="" required>
</label>
<input type="button" onclick="confirmSlotBooking()" value="Confirm Slot">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var inputDatetimeLocal = document.getElementById("datetimeLocalSelect");
   divDisplay.textContent = 'Slot Required: '+inputDatetimeLocal.required;
   function confirmSlotBooking() {
      if(inputDatetimeLocal.value === '')
         divDisplay.textContent += ', Choose a valid Exam Slot. ';
      else
         divDisplay.textContent = 'Slot Confirmed: '+inputDatetimeLocal.required+', Your Slot: '+inputDatetimeLocal.value;
   }
</script>
</body>
</html>

Output

This will produce the following output −

Clicking ‘Confirm Slot’ button −

After clicking ‘Confirm Slot’ button with value of datetimeLocal field set −

raja
Updated on 30-Jul-2019 22:30:26

Advertisements