小程序数字类型加小数点两位
<input type="digit" placeholder="请输入自定义金额" bindblur='price_no_focus' bindfocus="price_focus" value="{{price}}" bindinput="handleInput"/>
formatNum(e) { //正则验证金额输入框格式 e.detail.value = e.detail.value.replace(/^(\-)*(\d+)\.(\d{2}).*$/, '$1$2.$3')//只能输入两个小数 e.detail.value = e.detail.value.replace(/[\u4e00-\u9fa5]+/g, ""); //清除汉字 e.detail.value = e.detail.value.replace(/[^\d.]/g, ""); //清楚非数字和小数点 e.detail.value = e.detail.value.replace(/^\./g, ""); //验证第一个字符是数字而不是 e.detail.value = e.detail.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); //只保留第一个小数点, 清除多余的 }, handleInput:function(e){ var that = this; that.formatNum(e); that.data.price = e.detail.value; that.setData({ price: that.data.price }) },
标签:两位,数字,price,detail,value,replace,小数点 From: https://www.cnblogs.com/yu412/p/14756263.html