Alert some text if an input field contains less than 6 characters:
If you submit the form with less than 6 characters, 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"> Name: <input type="text" id="myInput" name="fname" pattern=".{6,}" required> <input type="submit" value="Submit"> </form>/* w w w . j a v a 2 s . co m*/ <p id="demo"></p> <script> document.getElementById("myInput").addEventListener("invalid", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "Must contain 6 or more characters"; } </script> </body> </html>