inputNum: function (e) {
return this.checkInputText(e.detail.value);
}, //检查输入文本,限制只能为数字并且数字最多带2位小数
checkInputText: function (text) {
var reg = /^(\.*)(\d+)(\.?)(\d{0,2}).*$/g; if (reg.test(text)) { //正则匹配通过,提取有效文本
text = text.replace(reg, ‘$2$3$4‘);
} else { //正则匹配不通过,直接清空
text = ‘0.‘;
} return text; //返回符合要求的文本(为数字且最多有带2位小数)
},
<view class=‘row‘>
<view class="title">文本输入限制输入数字,且最多带2位小数</view>
<input type=‘text‘ placeholder=‘请输入有效数字‘ bindinput=‘inputNum‘ class=‘numInput‘ style="border:1px solid red;"/>
</view>