[英]Restrict decimal and allowing only number
我正在尝试通过限制小数来验证仅允许数字的文本字段,但由于某种原因它也接受小数。
<input type="text" id="textbox" maxlength="10" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');"></input>
这样做:
*需要Javascript
只允许数字和退格键
<input type="text" onkeypress="return (event.charCode.=8 && event.charCode ==0 || (event.charCode >= 48 && event.charCode <= 57))" />
现在检查
<input type="text" oninput="this.value = this.value.replace(/[^0-9]/, '')" />
您最初的解决replace
很接近 - 如果您只是删除.
从第一次replace
中的否定匹配:
<input type="text" id="textbox" maxlength="10" oninput="this.value = this.value.replace(/[^0-9]/g, '');"></input>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.