vue中。input输入框,限制1位小数,限制2位小数,不限制小数,限制不带小数点的数字

时间:2022-01-17 00:55:07


methods: {
vSeachNumber1(prop) { // 不带小数点的数字
this.dataForm[prop] = this.dataForm[prop].replace(/[^\d]/g, '')
},
vSeachPrice(prop) { // 带小数点的数字
this.dataForm[prop] = this.dataForm[prop].replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
},
// 限制两位小数
vSeachNumber(prop) {
this.dataForm[prop] = this.dataForm[prop].replace(
/^(\-)*(\d+)\.(\d\d).*$/,
"$1$2.$3"
);
},
//限制一位小数
numberCheck(prop) {
var str = this.dataForm[prop]
var len1 = str.substr(0, 1);
var len2 = str.substr(1, 1);
//如果第一位是0,第二位不是点,就用数字把点替换掉
if (str.length > 1 && len1 == 0 && len2 != ".") {
str = str.substr(1, 1);
}
//第一位不能是.
if (len1 == ".") {
str = "";
}
//限制只能输入一个小数点
if (str.indexOf(".") != -1) {
var str_ = str.substr(str.indexOf(".") + 1);
if (str_.indexOf(".") != -1) {
str = str.substr(0, str.indexOf(".") + str_.indexOf(".") + 1);
}
}
//正则替换,保留数字和小数点
str = str.match(/^\d*(\.?\d{0,1})/g)[0] || null;
this.dataForm[prop] = str;
},
}