源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p>輸入數字併點擊驗證按鈕:</p> <input id="id1" type="number" max="100"> <button onclick="myFunction()">驗證</button> <p>如果輸入的數字大於 100 ( input 的 max 屬性), 會顯示錯誤信息。</p> <p id="demo"></p> <script> function myFunction() { var txt = ""; if (document.getElementById("id1").validity.rangeOverflow) { txt = "輸入的值太大了"; } else { txt = "輸入正確"; } document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
運行結果