如何验证包含 4 个字母但不包括“.”的字符串特点?

How to validate a string that contains 4 letter not including '.' character?

我正在尝试验证文本字段值,以便最多包含 4 个字符(不包括“.”)字符.

<v-text-field 
   v-model="diagnoseCode" 
   style="height: 38px"
   outlined 
   dense 
   maxLength="4">
</v-text-field>

我试过了,但这没有考虑检查“.”。特点。 如有任何帮助,我们将不胜感激。

您可以使用方法来设置最大长度:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data() {
    return {
      diagnoseCode: null,
      max: null
    }
  },
  methods: {
    clear() {
      this.max = null
    },
    check(e) {
      str = this.diagnoseCode
      if (str.replaceAll('.', '').length > 4 && !this.diagnoseCode.endsWith('.')) {
        this.diagnoseCode = this.diagnoseCode.slice(0,-1)
        this.max = this.diagnoseCode.length
      }
    }
  },
})
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<div id="app">
  <v-app>
    <v-main>
      <v-container>
        <v-text-field v-model="diagnoseCode"
                      style="height: 38px"
                      outlined 
                      dense 
                      :maxLength="max"
                      @keyup="check($event.target.value)"
                      @keydown="clear">
        </v-text-field>
      </v-container>
    </v-main>
  </v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>