Check if an input field contains a number that is less than 2 or greater than 5: - Javascript DOM Event

Javascript examples for DOM Event:oninvalid

Description

Check if an input field contains a number that is less than 2 or greater than 5:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php" method="get">
  Pick a number between 2 and 5: <input type="number" id="myInput" name="quantity" min="2" max="5">
  <input type="submit" value="Submit">
</form>/*from   w w w  .java2 s . c  o  m*/

<script>
document.getElementById("myInput").addEventListener("invalid", myFunction);

function myFunction() {
    console.log("You must pick a number between 2 and 5. You chose: " + this.value);
}
</script>

</body>
</html>

Related Tutorials