javascript - 接受带逗号和点的小数 (html)
问题描述
我需要一些有关此输入的帮助,因为我希望它只接受 2 个小数和逗号或点,但现在只允许逗号而不是点和任何小数。我对 RegExp 非常陌生,我正在尝试这个。
<td>
<input type="number" ng-model="material.porcentaje" ng-change="calculaKilos(material, $index);validatePorcentaje($index)" id="porcentaje" class="input_small-stretch" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
</td>
解决方案
您不能添加,
到type="number"
. 您需要将其更改为type="text"
,然后onkeyup
您可以检查是否有除数字和逗号以外的任何值,您可以将其替换为''
document.querySelector('input').addEventListener('keyup',(e)=>{
let value = e.target.value;
e.target.value = value.replace(/[^0-9.,]/g,'');;
})
<input type="text" ng-model="material.porcentaje" ng-change="calculaKilos(material, $index);validatePorcentaje($index)" id="porcentaje" class="input_small-stretch" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
希望它会有所帮助
推荐阅读
- sql - 如何将 4 个 sql 查询组合成一个性能良好的查询?
- facebook - Facebook 连接帐户(PSID、ASID)
- oracle - Oracle updatexml 在大量数据上抛出 pga 内存
- regression - 多项式特征的引入不会导致共线性增加吗?
- assembly - EFLAGS 状态
- c# - 检查是否在浏览器中为某些站点设置了内容安全策略
- python - 如何在使用 Maskrcnn 屏蔽后提取分段对象
- go - 如何在一台机器上在consul中注册多个服务实例
- git - git push 永远不会弹出 GitHub 登录窗口
- python - 如何更新嵌套列表中的值?