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();" />
就是在失去焦点时也进行验证,因为可以用输入法输入汉字(用鼠标选取汉字)或者用鼠标进行粘贴操作
推荐阅读
-
php限制只能输入英文字符和数字的正则表达式
-
iOS UITextField、UITextView只限输入中文、英文、数字及实时限制字符个数的封装实现代码
-
iOS 键盘输入限制(只能输入字母,数字,禁止输入特殊符号)
-
使用UITextField限制只可输入中,英文,数字的方法
-
使用UITextField限制只可输入中,英文,数字的方法
-
C#中winform控制textbox输入只能为数字的方法
-
C#中winform控制textbox输入只能为数字的方法
-
兼容firefox的文本框只能输入两位小数的数字的代码
-
js限制文本框只能输入数字(正则表达式)
-
C# TextBox控件实现只能输入数字的方法