接受带逗号和点的十进制(html)

时间:2019-02-04 20:57:55

标签: javascript html angularjs

我需要一些帮助,因为我希望它只接受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>

2 个答案:

答案 0 :(得分: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})?$/">

希望这会有所帮助

答案 1 :(得分:0)

签出RegExr来帮助建立您的正则表达式模式。我可以尝试为您构建模式,但是如果没有示例文本,这将很困难。