- tool.js文件中
export function inputLimit(e, point) {
if (point > 0) {
e.target.value = e.target.value.replace(/[^\d.]/g, "")
.replace(/^\./g, "")
.replace(/\.{2,}/g, ".")
.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".")
.replace(new RegExp(`^(\\-)*(\\d+)\\.(\\d{1,${point}}).*$`), '$1$2.$3');
} else {
if (e.target.value.length == 1) {
e.target.value = e.target.value.replace(/[^0-9]/g, "");
} else {
e.target.value = e.target.value.replace(/[^\d]/g, "");
}
}
if (e.target.value.indexOf(".") < 0 && e.target.value != "") {
e.target.value = parseFloat(e.target.value);
}
return e.target.value;
}
- main.js文件中
import { inputLimit } from "./server/tool"
Vue.prototype.$inputLimit = inputLimit
- 页面中使用
<!--不传参数表示只能传整数-->
<Input class="width" v-model="formValidate.duoHoldMax" @input.native="$inputLimit" />
<!--传的第二个参数表示要保留几位小数->
<Input v-model="obj.auditLimit" style="width: 200px; margin: 0 10px" @on-keyup="$inputLimit($event, 10)"/>