W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>JavaScript 验证</h1> <p>请输入一个数字然后点击 OK:</p> <input id="id1" type="number" min="100" max="300" required> <button onclick="myFunction()">OK</button> <p>如果数字小于 100 或大于 300,将显示错误消息。</p> <p id="demo"></p> <script> function myFunction() { const inpObj = document.getElementById("id1"); if (!inpObj.checkValidity()) { document.getElementById("demo").innerHTML = inpObj.validationMessage; } else { document.getElementById("demo").innerHTML = "Input OK"; } } </script> </body> </html>