Javascript DOM HTML Input Number required Property get

Introduction

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

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

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

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Number: <input type="number" id="myNumber" name="quantity" required>
  <input type="submit">
</form>//from  www .j ava 2 s  .  c o m
<button onclick="myFunction()">Test</button>
<p id="demo"></p>

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

</body>
</html>

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

This property mirrors the HTML required attribute.

The required property accepts and returns a boolean type value.

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

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




PreviousNext

Related