uniapp input框只允许输入两位小数、v-model双向数据绑定失效、正则校验

269 阅读1分钟

正则替换:

<input type="digit" v-model="value" @input="inputChange" />

inputChange(e){
    const inputType = /[^\d]/g   //想限制什么类型在这里换换正则就可以了
    this.$nextTick(function() {
        this.value = e.detail.value.replace(inputRule, '');
    })
}
// 只能输入数字
const inputType = /[^\d]/g		
// 只能输入字母
const inputType = /[^a-zA-Z]/g		
// 只能输入数字和字母
const inputType =/[\W]/g
// 只能输入小写字母
const inputType =/[^a-z]/g
// 只能输入大写字母
const inputType =/[^A-Z]/g
// 只能输入数字和字母和下划线
const inputType =/[^\w_]/g //下划线也可以改成%
// 只能输入中文
const inputType =/[^\u4E00-\u9FA5]/g
// 只能输入数字和小数点
const inputType =/[^\d.]/g
// 中文、英文、数字
const inputType = /[^\u4E00-\u9FA5\a-zA-Z\d]/g;

参考链接:devpress.csdn.net/viewdesign/…

保留2位小数

<input type="digit" v-model="rechargeMoney" @input="inputChange" />

inputChange(e){
      e.detail.value = (e.detail.value.match(/^\d*(\.?\d{0,2})/g)[0]) || null // 保留2位小数
      this.$nextTick(() => { // 解决双向数据绑定失效问题
            that.rechargeMoney= e.detail.value
      })
}

参考链接:blog.csdn.net/qq_40549954…