Javascript Reference - HTML DOM Input Time required Property








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

Browser Support

required Yes No No Yes Yes

Syntax

Return the required property.

var v = timeObject.required 

Set the required property.

timeObject.required=true|false

Property Values

Value Description
true|false Set whether a time field should be a required field of form submission.
  • true - The time field is required
  • false - Default. The time field is not required




Return Value

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

Example

The following code shows how to set a time field to be required.


<!DOCTYPE html>
<html>
<body>
<!--from w w w.j  a  va  2s .  co  m-->
<form action="url">
  Time: <input type="time" id="myTime" name="usr_time">
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myTime").required = true;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

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


<!DOCTYPE html>
<html>
<body>
<!-- ww w.j ava2  s  .c o  m-->
<form action="url">
  Time: <input type="time" id="myTime" name="usr_time" required>
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

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

</body>
</html>

The code above is rendered as follows: