关于vue.js:最大3位整数一位小数

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理