一、說明javascript
本篇文章介紹怎麼使用js限制文本框只能輸入數字html
二、HTML代碼java
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript"> function validationNumber(e, num) { var regu = /^[0-9]+\.?[0-9]*$/; if (e.value != "") { if (!regu.test(e.value)) { alert("請輸入正確的數字"); e.value = e.value.substring(0, e.value.length - 1); e.focus(); } else { if (num == 0) { if (e.value.indexOf('.') > -1) { e.value = e.value.substring(0, e.value.length - 1); e.focus(); } } if (e.value.indexOf('.') > -1) { if (e.value.split('.')[1].length > num) { e.value = e.value.substring(0, e.value.length - 1); e.focus(); } } } } } </script> </head> <body> <form id="form1" runat="server"> <div> <input type="text" onkeyup="validationNumber(this,0)" name="id1" value="" /> </div> </form> </body> </html>