Alert some text if an input field contains a number that is less than 2 or greater than 5:
If you submit the form with a number that is less than 2 or greater than 5, an alert message will occur.
The oninvalid event is not supported in Internet Explorer 9 and earlier or Safari.
<!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" required> <input type="submit" value="Submit"> </form>//from w w w . ja va 2s.c o m <p id="demo"></p> <script> document.getElementById("myInput").addEventListener("invalid", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "You must pick a number between 2 and 5. You chose: " + this.value; } </script> </body> </html>