<html>
<body>
<form action="/action_page.php" method="get">
Name: <input type="text" id="myInput" name="fname" pattern=".{6,}" required>
<input type="submit" value="提交">
</form>
<p>如果您提交的表单少于 6 个字符,则会出现一条警告消息。</p>
<p><strong>注意:</strong> Internet Explorer 9 及更早版本或 Safari 不支持 oninvalid 事件。</p>
<script>
document.getElementById("myInput").addEventListener("invalid", myFunction);
function myFunction() {
alert("Must contain 6 or more characters");
}
</script>
</body>
</html>