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

dragonishare
157 声望3 粉丝

« 上一篇
Vue全屏组件
下一篇 »
markdown-it