如何保證移動(dòng)端的vue項(xiàng)目,在input中只能輸入金額?包括數(shù)字和小數(shù)點(diǎn)。 發(fā)現(xiàn)如果調(diào)type=‘tel’的話只有數(shù)字沒(méi)有小數(shù)點(diǎn),調(diào)type=‘number’還有其他符號(hào)。 監(jiān)聽(tīng)input動(dòng)作來(lái)去掉符號(hào)發(fā)現(xiàn)也不行,不知道是不是自己代碼的問(wèn)題。<input ref="content" type="number" pattern="[0-9]{,6}" @input="handleAmountChange" v-model.number.trim="money" /> handleAmountChange(e) { this.money = e.target.value.replace(/[^\d]/g, '') // 必須保證第一個(gè)為數(shù)字而不是. this.money = this.money.replace(/^\./g, '0.') // 保證只有出現(xiàn)一個(gè).而沒(méi)有多個(gè). this.money = this.money.replace(/\.{2,}/g, '.') // 保證.只出現(xiàn)一次,而不能出現(xiàn)兩次以上 this.money = this.money .replace('.', '$#$') .replace(/\./g, '') .replace('$#$', '.') // 只能輸入兩個(gè)小數(shù) this.money = this.money.replace(/^()*(\d+)\.(\d\d).*$/, '$1$2.$3') },
如何保證移動(dòng)端vue只能輸入金額?
元芳怎么了
2019-03-21 18:15:27