小程序/js监听输入框验证金额
refundAmoutInput: function(event){ var value = event.detail.value; if (value.split('.')[0].length > 7) { if (value.split('.')[1]) { value = value.split('.')[0].substr(0, value.split('.')[0].length - 1) + '.' + value.split('.')[1]; } else { value = value.split('.')[0].substr(0, value.split('.')[0].length - 1); } } value = value.replace(/[^\d\.]/g, ""); //清除"数字"和"."以外的字符 value = value.replace(/^\./g, ""); //验证第一个字符是数字 value = value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的 value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); var refundAmoutStr = 'refundInfo.refundAmout'; this.setData({ [refundAmoutStr]: value, }) },
最多7位整数+最多两位小数点,如超出位数或者输入其它格式,不显示输入;