不错的一个用正则检测输入的字符是否为数字的代码,也是一种并不常见的写法
Untitled Document function $(obj){ return document.getElementById(obj); } function checkIsInteger(str) { //如果为空,则通过校验 if(str == "") return true; if(/^(\-?)(\d+)$/.test(str)) return true; else return false; } String.prototype.trim = function() { return this.replace(/(^[\s]*)|([\s]*$)/g, ""); } $("myButton").onclick=function(){ if(checkIsInteger($("myInput").value.trim())){ alert("成功"); }else{ alert("只能是数字"); } } [Ctrl+A 全选 注: 如需引入外部Js需刷新才能执行 ]
查看更多关于javascript 正则 判断是否是数字的脚本_正则表达式的详细内容...