<html> <head> <script type="text/javascript"> function test(){ var obj = document.getElementById('test'); if(!isNaN(obj.value)){ return ture; }else{ alert("您输入的不是数字,请重新输入!"); obj.value="";} } </script> </head> <body> <input type="text" id="test" onpropertychange="if(/[^0-9\\-]/ig.test(value))value=value.replace(/[0-9^\\-]/ig,'')" oninput="value=value.replace(/[^0-9\\-]/ig,'')" style="ime-mode: disabled" ><input type="button" onclick="test()" value="提交"/> </body> </html>
<html><head> <title></title> <script type="text/javascript"> function reg() { var inp= document.getElementById("sr"); var reg=/^[0-9]+$/; var result= reg.test(inp.value); if(!result) {alert("请输入数字!"); inp.value=""; } } </script></head><body> <input id="sr" type="text" onchange="reg()" /></body></html>