接受逗号和点分十进制(HTML)

问题描述 投票:2回答:2

我需要一些帮助,这是因为输入我想它只能接受2位小数,并用逗号或点为好,但现在只允许逗号,而不是一个点和任何小数。我用正则表达式非常新,我是这个尝试。

<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>
javascript html angularjs
2个回答
0
投票

退房RegExr,以帮助建立你的正则表达式模式。我可以尝试建立模式给你,但没有示例文本是困难的。


0
投票

您不能添加到, 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})?$/">

希望这将有助于

© www.soinside.com 2019 - 2024. All rights reserved.