Javascript DOM HTML Input Month required Property get

Introduction

Find out if a month field must be filled out before submitting a form:

var x = document.getElementById("myMonth").required;

Click button to find out if the month field must be filled out before submitting the form.

input elements with type=month is not supported in Firefox.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Month: <input type="month" id="myMonth" name="bdaymonth" required>
  <input type="submit">
</form>//w w w  .  ja  v a 2 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 required property sets or gets whether a month field must be filled out before submitting a form.

This property mirrors the HTML required attribute.

The required property accept and returns a boolean value.

Value Description
true The month field is a required part of form submission
false Default. The month field is not a required part of form submission

The required property returns true if the month field is a required part of form submission, otherwise it returns false.




PreviousNext

Related