Javascript Reference - HTML DOM Input Month required Property








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

Browser Support

required Yes No No Yes Yes

Syntax

Return the required property.

var v = monthObject.required 

Set the required property.

monthObject.required=true|false

Property Values

Value Description
true|false Set whether a month field should be a required field.
  • true - The month field is a required field
  • false - Default. The month field is not a required field




Return Value

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

Example

The following code shows how to set a month field to a required field.


<!DOCTYPE html>
<html>
<body>
<!--from  ww  w  .  j a  v a  2 s . c o m-->
<form action="url">
  Month: <input type="month" id="myMonth" name="bdaymonth">
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myMonth").required = true;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

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


<!DOCTYPE html>
<html>
<body>
<form action="url">
  Month: <input type="month" id="myMonth" name="bdaymonth" required>
  <input type="submit">
</form><!--  w w  w  .  j ava2 s .  c  o m-->
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myMonth").required;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: