Input Number required Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Number

Description

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

This property reflects the HTML required attribute.

Set the required property with the following Values

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

Return Value

A Boolean, returns true if the number field is required, otherwise it returns false

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form action="#">
  Number: <input type="number" id="myNumber" name="quantity">
  <input type="submit">
</form>/*  www .  j a  v  a 2s .co m*/

<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

Related Tutorials