<template> <div class="content"> <input type="text" placeholder="请输出数字" class="input-area" v-model.trim="tonnage" maxlength="50" @input="dealInputVal(tonnage)" /> </div></template><script>export default { components: {}, data() { return { tonnage: '', } }, mounted() {}, methods: { dealInputVal(num) { //最大3位整数,一位小数; 100非法,100.1非法,1000不非法,100.11不非法 this.tonnage = num .replace(/[^\d.]/g, '') .replace(/\.{1,}/g, '.') .replace(/^(0(0)+|\.)/g, '') .replace('.', '$#$') .replace(/\./g, '') .replace('$#$', '.') .replace(/^(-)*(\d+)\.(\d).*$/, '$1$2.$3') if (parseInt(this.tonnage) >= 1000) { this.tonnage = '999' } } }}</script><style lang="less" scoped></style>