Javascript Reference - HTML DOM Input Date required Property








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

Browser Support

required Yes No No Yes Yes

Syntax

Return the required property.

var v = dateObject.required 

Set the required property.

dateObject.required=true|false

Property Values

Value Description
true|false Specifies whether a date field is required for form submission.
  • true - The date field is required for form submission
  • false - Default. The date field is not required for form submission




Return Value

A Boolean type value, true if the date field is required for form submission, otherwise it returns false.

Example

The following code shows how to mark a date field as required.


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

The code above is rendered as follows:





Example 2

The following code shows how to check if a date field must be filled before submitting a form.


<!DOCTYPE html>
<html>
<body>
<!-- ww w  .  j a  v  a  2s  . com-->
<form action="form_action.asp">
  Date: <input type="date" id="myDate" name="date" required>
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myDate").required;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: