In JavaScript, using the addEventListener()
method:
If you click submit, without filling out the text field, an alert message will occur.
object.addEventListener("invalid",
myScript);
<!DOCTYPE html> <html> <body> <form action="/action_page.php" method="get"> Name: <input type="text" id="myInput" name="fname" required> <input type="submit" value="Submit"> </form>/*from ww w . ja va2s .com*/ <p id="demo"></p> <script> document.getElementById("myInput").addEventListener("invalid", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "You must fill out the form!"; } </script> </body> </html>
Bubbles: | No |
---|---|
Cancelable: | Yes |
Event type: | Event |
Supported HTML tags: | <input> |