iview input金額校驗

要求

  • 只能輸入數字
  • 最多一個小數點

代碼

<Input type="text" v-model="ipt" />

that.$nextTick(() => {
    if(this.ipt) {
    
        // 如果輸入的有字符串,把字符串轉化爲數字
        if(/[^\d\.]/.test(that.ipt)) {                              
            that.ipt= that.ipt.replace(/[^\d\.]/g, '');
        } 
        
        // 如果輸入了多個小數點
        if(that.ipt.split('.').length > 2) {
            that.ipt= that.ipt.split('.')[0] + '.' + that.ipt.split('.')[1];
        }

        // 如果和上一次的值相等,就不處理
        // if(Number(that.ipt) == Number(that.iptOrigin)) {
        //    return;
        // }
    }
})

// 如果把數據刪了,過一會再賦值,因爲萬一是刪掉輸入0.5呢
settimeout(() => {
	 // 如果把值刪了
        if(!that.ipt) {
            that.ipt= 1;  // 初始值
        }

        if(that.ipt == '.') {
            that.ipt= 1
        }
}, 1500)

一定要放到nexttick裏面!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章