小程序文本框保留兩位小數(非四舍五入)
阿新 • • 發佈:2018-12-08
row repl true 通過 UNC bind 小數 text urn
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>
小程序文本框保留兩位小數(非四舍五入)