vue

当输入框只允许输入小数的el-input正则

2024-06-17  本文已影响0人  Morbid_D

<el-input v-model="balanceRechargePrices" placeholder="请输入充值金额" @input="handleInput" @focus="focusInput"></el-input>
const handleInput = (value: string) => {
console.log('value', value);
const restrictedValue = value.replace(/[^0-9.]/g, '').replace(/^./, '');
// 限制小数点后最多两位
let limitedValue = restrictedValue.split('.').length > 1 ? ${restrictedValue.slice(0, restrictedValue.indexOf('.') + 3)} : restrictedValue;
limitedValue = limitedValue.replace(/(..*)./g, '$1');
balanceRechargePrice.value = limitedValue ? limitedValue : 0;
balanceRechargePrices.value = limitedValue ? limitedValue : '';
};
const focusInput = () => {
balanceRechargePrice.value = balanceRechargePrices.value;
};

上一篇 下一篇

猜你喜欢

热点阅读