欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

input 限制只能输入数字

程序员文章站 2022-06-07 13:37:50
...
<input type="text" onkeyup="this.value=this.value.replace(/[^0-9-]+/,'');" />
这是你想要的结果,不过我建议改成:
<input type="text" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v();" />
就是在失去焦点时也进行验证,因为可以用输入法输入汉字(用鼠标选取汉字)或者用鼠标进行粘贴操作
相关标签: html js