小程序版
<template>
<view>
<input placeholder="请输入金额" type="digit" placeholder-class="phcolor" value="{{amount}}" data-key="mobile" @input="handleInput"></input>
</view>
</template>
methods = {
handleInput(e) {
var obj = e.detail.value
obj = obj.replace(/[^\d.]/g, '') //清除“数字”和“.”以外的字符
obj = obj.replace(/\.{2,}/g, '.') //只保留第一个. 清除多余的
obj = obj
.replace('.', '$#$')
.replace(/\./g, '')
.replace('$#$', '.')
obj = obj.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') //只能输入两个小数
if (obj.indexOf('.') < 0 && obj != '') {
//以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
obj = parseFloat(obj)
}
this.amount = obj
},
}