
/**
* 开头是 . 会转成0.
* 开头是0 再次输入数字会替换0
* 最多输入2位小数点
*/
const input = document.querySelector('input').
addEventListener('input', function () {
this.value = this.value.replace(/[^\d.]/, '')
.replace(/^[\.]/, '0.')
.replace(/^0(\d)/, '')
.replace(/^([\.]){2,}/, '0.')
.replace('.', '$#$')
.replace(/\./g, '')
.replace('$#$', '.')
.replace(/^(\d*)(\.?\d{2})\d*$/, '');
});
``
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)