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

3次阅读

共计 598 个字符,预计需要花费 2 分钟才能阅读完成。

<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>
正文完
 0