美文网首页vue
当输入框只允许输入小数的el-input正则

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

作者: Morbid_D | 来源:发表于2024-06-17 09:55 被阅读0次

<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;
};

相关文章

网友评论

    本文标题:当输入框只允许输入小数的el-input正则

    本文链接:https://www.haomeiwen.com/subject/vfhxcjtx.html