Javascript Reference - HTML DOM Input DatetimeLocal required Property








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

Browser Support

required Yes No No Yes Yes

Syntax

Return the required property.

var v = datetimelocalObject.required 

Set the required property.

datetimelocalObject.required=true|false

Property Values

Value Description
true|false Set whether a local datetime field is a required part of the form.
  • true - The local datetime field is required
  • false - Default. The local datetime field is not required




Return Value

A Boolean type value, true if the local datetime field is a required part of form submission, otherwise false.

Example

The following code shows how to set a local datetime field to be a required part of form submission.


<!DOCTYPE html>
<html>
<body>
<!-- w ww.ja  va2  s  .c om-->
<form action="url">
  Date: <input type="datetime-local" id="myLocalDate" name="date">
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myLocalDate").required = true;
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

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


<!DOCTYPE html>
<html>
<body>
<form action="url">
  Date: <input type="datetime-local" id="myLocalDate" name="date" required>
  <input type="submit">
</form><!--from   www.j  a  va2  s.  c o m-->
<button onclick="myFunction()">test</button>
<p id="demo"></p>

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

The code above is rendered as follows: