<el-input
placeholder="请输入"
size="medium"
v-model="amt"
maxlength="16"
:disabled="showBtn === 'no' || readOnly"
oninput="
this.value = this.value.replace(/[^\d.]/g,'')
.replace(/\.{2,}/g, '.')
.replace(/^0+(\d)/, '$1') // 第一位0开头,0后面为数字,则过滤掉,取后面的数字
.replace(/^\./, '') // 如果输入的第一位为小数点,则替换成 0. 实现自动补全
.replace(/^(\d+)\.(\d\d).*$/, '$1.$2')"
@keyup.native="($event) => $keyupEvent($event)"
@blur="changeNumAmt('apply_amt', $event)"
@focus="backNum('apply_amt', $event)"
/>
//键盘事件(限制操作 小数点、删除键,输入其他内容将输入框数据清空)
export function keyupEvent(e) {
// console.log('调用键盘事件',e);
let keyAry = ['Backspace', 'Delete', 'Period','NumpadDecimal']
let numObj = {
0: 'Digit',
1: 'Numpad',
}
let reg = /\d+|\./
let charCode = ''
if (keyAry.includes(e.code)) {
charCode = '.'
} else {
for (let i in numObj) {
if (e.code.indexOf(numObj[i]) > -1) {
charCode = e.code.substr(e.code.length - 1, 1)
break
} else {
let regex = /^[A-Za-z]$/
if (regex.test(e.key)) {
charCode = ''
break
} else {
charCode = '.'
}
}
}
}
if (!reg.test(charCode)) {
e.target.value = ''
}
}
* @description: 格式化金额处理
* @return {*}
*/
changeNumAmt(val, e) {
this.Data[val] = e.target.value ? amoThreeForm(e.target.value) : ''
},
* @description: 输入框进入光标回显金额处理
* @return {*}
*/
backNum(val, e) {
this.Data[val] = e.target.value ? threeToNum(this.Data, val) : ''
},
//数字转千分位 eg:1,111,222.90
export function amoThreeForm(numb){
// 转为字符串,并按照.拆分
const arr = (numb + "").split(".");
// 整数部分再拆分
const int = arr[0].split("");
// 小数部分
const fraction = arr[1] || "";
// console.log(fraction.length,"小数长度");
var result;
if (fraction.length ==0) {
result = '00'
}else if (fraction.length ==1) {
result = fraction.toString()+'0';
}else{
result = fraction.toString().substring(0, fraction.toString().indexOf(".") + 3)
}
// 返回的变量
let r = "";
int.reverse().forEach(function (v, i) {
// 非第一位并且是位值是3的倍数,添加“,”
if (i !== 0 && i % 3 === 0) {
r = v + "," + r;
} else {
// 正常添加字符(这是好写法)
r = v + r;
}
});
// 整数部分和小数部分拼接
console.log(r + (!!result ? "." + result : ""),"转换后的值====================")
return r + (!!result ? "." + result : "");
}
//回显前分位转数字 eg:111222.90
export function threeToNum(from,val){
let reg = new RegExp(",", "g");
if (from[val] != "") {
let falg = from[val].indexOf(".00") !=-1;//证明有.00
if (falg) {
from[val] = from[val].slice(0, -3);
}
return from[val].replace(reg, "");
}
}
利率处理
<el-input
maxlength="6"
placeholder="请输入"
size="medium"
v-model="amt"
oninput="
this.value=this.value.replace(/^\D*(\d*\.?\d{0,2})?.*$/, '$1') //限制小数点后两位
.replace(/^(\d{0,3})?\d*$/, '$1') //限制两位整数
.replace(/\b\d{4,}\b/g, '');
"
@blur="handlblur('amt', $event)"
/>
/**
* @description: 利率处理
* @param {*} e
* @return {*}
*/
handlblur(name, e) {
console.log('handlblur', name, e.target.value)
this.Data[name] = Number(e.target.value).toFixed(2) == 0 ? '' : Number(e.target.value).toFixed(2)
},