HTML DOM Input Date required Property

HTMLWeb DevelopmentFront End Technology

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

Syntax

Following is the syntax −

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

Boolean Values

Here, “booleanValue” can be the following −

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

Example

Let us see an example of Input Date required property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Input Date required</title>
</head>
<body>
<form>
<div>
Name: <input type="text" name="fullName">
</div>
<div>
Date of Birth: <input type="date" id="dateSelect" required>
</div>
</form>
<button onclick="submit()">Confirm</button>
<div id="divDisplay"></div>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var inputDate = document.getElementById("dateSelect");
   function submit() {
      if(inputDate.required==true && inputDate.value==='')
         divDisplay.textContent = 'DOB required';
      else
         divDisplay.textContent = 'Details Received';
   }
</script>
</body>
</html>

Output

This will produce the following output −

Clicking ‘Confirm’ button −

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

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

Advertisements