javascript - 如何在此输入中仅输入数字和减号
问题描述
<div class="kinput">
<input type="text" placeholder="Enter value" maxlength="7" id="kinput" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
</div>
我找到了这个表达
oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');"
但它不允许我输入减号。如果有办法我可以包含减号而不允许其他非数字字符..
解决方案
您可以将 包含-
在正则表达式 ( [^0-9.-]
) 中以忽略匹配中的该字符,并在替换中跳过该字符。
<div class="kinput">
<input type="text" placeholder="Enter value" maxlength="7" id="kinput" oninput="this.value = this.value.replace(/[^0-9.-]/g, '').replace(/(\..*)\./g, '$1');">
</div>
推荐阅读
- ruby-on-rails - 使用 rspec 在另一个方法中测试该方法是否被调用
- javascript - 节点红色我无法提取消息正文
- javascript - 具有垫表初始化的角度材料2分页器
- autodesk-forge - Postman 中的“获取清单”状态失败
- google-chrome - 谷歌浏览器:刷新重复的标签
- r - 比较同一数据框中的组中的行
- testing - 自动检查输出与预期输出的软件(内部图片)
- javascript - range.getBoundingClientRect() 在选择一个节点并折叠范围后为所有值返回零
- laravel - 如何在 Laravel 5.6 中添加多于两列来处理多态关系
- ibm-mq - MQ 管理器自动结束