Javascript DOM HTML Input Number required Property set

Introduction

Set a number field to be a required part of form submission:

document.getElementById("myNumber").required = true;

Click the button to set the required property for the number field.

View in separate window

<!DOCTYPE html>
<html>
<body>

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

<script>
function myFunction() {
  document.getElementById("myNumber").required = true;
  document.getElementById("demo").innerHTML = "The required property was set.";
}
</script>

</body>
</html>



PreviousNext

Related