方法2、經過寫JS進行判斷控制輸入的只能爲數字,源碼以下:html
<!DOCTYPE html> <html> <body> <h1></h1> <p>請輸入數字。若是輸入值不是數字,瀏覽器會彈出提示框。</p> <input id="demo" type="text"> <script> function myFunction() { var x=document.getElementById("demo").value; if(x==""||isNaN(x)) { alert("Not Numeric"); } } </script> <button type="button" onclick="myFunction()">點擊這裏</button> </body> </html>
這個方法能夠在W3C上面查到,這裏就很少說了,很是簡單,但有時候須要用到另一種方法能夠借鑑下。瀏覽器