小编Doy*_*oun的帖子

有没有办法通过 NOT REQUIRED v-text-field 规则的验证?

我试图验证 v-text-field 仅用于输入数字,但这不是必需的,但规则拒绝通过验证。

我使用了 v-form、v-text-field 和 v-text-field 的规则。

<template>
  <v-form ref="form">
    <v-text-field
      v-model="name"
      :label="label"
      :rules="rules"
      @blur="changeValue"
      clearable
    ></v-text-field>
  <v-btn @click="send">submit</v-btn>
</v-form>
</template>

<script>
export default {
  data() {
    return {
      name: "",
      rules: [
        v =>
          v.length <= 50 || "maximum 50 characters",
        v =>
          (v.length > 0 && /^[0-9]+$/.test(v)) || "numbers only"
      ]
    };
  },
  methods: {
    changeValue(event) {
      this.$emit("changeValue", event.target.value);
    },
    send() {
      const valid = this.$refs["form"].validate(); // doesn't pass
      if (valid) {
        this.$store.dispatch("xxx", {
          ... …
Run Code Online (Sandbox Code Playgroud)

forms validation textfield vuetify.js

1
推荐指数
1
解决办法
1169
查看次数

标签 统计

forms ×1

textfield ×1

validation ×1

vuetify.js ×1