如何在vuetify中添加密码匹配验证?

abh*_*yak 1 validation vue.js vuetify.js

我正在尝试创建一个配置文件表单,其中包含两个字段 passwordrePassword。基本上,它们应该是相同的。

我尝试使用网络上找到的不同代码和不同方法。他们中有些人工作了,但是。它实际上与代码不匹配。

这是一段代码:

Profile.vue:

<v-layout>
        <v-flex xs12 sm6>
          <v-text-field                        
            v-model="password"
            :append-icon="show ? 'visibility' : 'visibility_off'"
            :rules="[rules.required, rules.min]"
            :type="show ? 'text' : 'password'"
            name="password"
            label="Enter Password"
            hint="At least 8 characters"
            counter
            @click:append="show = !show"
          ></v-text-field>
        </v-flex>

          <v-flex xs12 sm6>
            <v-text-field            
              v-model="rePassword"
              :append-icon="show1 ? 'visibility' : 'visibility_off'"
              :rules="[rules.required, rules.min]"
              :type="show1 ? 'text' : 'password'"
              name="input-10-1"
              label="Re-enter Password"
              hint="At least 8 characters"
              counter
              @click:append="show1 = !show1"
            ></v-text-field>
          </v-flex>
          </v-layout>
Run Code Online (Sandbox Code Playgroud)

脚本如下所示:

Profile.vue(脚本):

data() {
      return {
        show: false,
        show1: false,
        password: 'Password',
        rePassword: 'Password',
        rules: {
          required: value => !!value || 'Required.',
          min: v => v.length >= 8 || 'Min 8 characters',
          emailMatch: () => ('The email and password you entered don\'t match')
        },
        emailRules: [
          v => !!v || 'E-mail is required',
          v => /.+@.+/.test(v) || 'E-mail must be valid'
        ],
        date: new Date().toISOString().substr(0, 10),
        menu: false,
        items: ['male', 'female'],
        address: '',
        title: "Image Upload",
        dialog: false,
        imageName: '',
        imageUrl: '',
        imageFile: ''
      }
    },
    methods: {
      pickFile() {
        this.$refs.image.click()
      },            
          onFilePicked(e) {
        const files = e.target.files
        if(files[0] !== undefined) {
          this.imageName = files[0].name
          if(this.imageName.lastIndexOf('.') <= 0) {
            return
          }
          const fr = new FileReader ()
          fr.readAsDataURL(files[0])
          fr.addEventListener('load', () => {
            this.imageUrl = fr.result
            this.imageFile = files[0] // this is an image file that can be sent to server...
          })
        } else {
          this.imageName = ''
          this.imageFile = ''
          this.imageUrl = ''
        }
        },
    }
      ,
      validate() {
        if (this.$refs.form.validate()) {
          this.snackbar = true
        }
      },
      reset() {
        this.$refs.form.reset()
      }
Run Code Online (Sandbox Code Playgroud)

如何使用vuetify在验证中添加密码匹配功能。谢谢

ßãl*_*ãjî 10

最简单的方法是使用 v-model(密码和确认密码),无需使用计算

规则

:rules="[v => !!v || 'field is required']"
Run Code Online (Sandbox Code Playgroud)

或者

:rules="[(password!="") || 'field is required']"
Run Code Online (Sandbox Code Playgroud)

在密码中

<v-text-field label="Password*" v-model="password" type="password" required   :rules="[v => !!v || 'field is required']"></v-text-field>
         
       
Run Code Online (Sandbox Code Playgroud)

确认密码字段规则

 :rules="[(password === confirm_password) || 'Password must match']"
Run Code Online (Sandbox Code Playgroud)

代码:

 <v-text-field label="Confirm Password*" v-model="confirm_password" type="password"  required   :rules="[(password === confirm_password) || 'Password must match']"></v-text-field>
         


           
Run Code Online (Sandbox Code Playgroud)


itt*_*tus 5

您可以定义自定义规则:

computed: {
    passwordConfirmationRule() {
      return () => (this.password === this.rePassword) || 'Password must match'
    },
}
Run Code Online (Sandbox Code Playgroud)

并使用它

 <v-flex xs12 sm6>
    <v-text-field            
      v-model="rePassword"
      :append-icon="show1 ? 'visibility' : 'visibility_off'"
      :rules="[rules.required, rules.min, passwordConfirmationRule]"
      :type="show1 ? 'text' : 'password'"
      name="input-10-1"
      label="Re-enter Password"
      hint="At least 8 characters"
      counter
      @click:append="show1 = !show1"
    ></v-text-field>
  </v-flex>
Run Code Online (Sandbox Code Playgroud)


Fra*_*ter 5

VeeValidate 非常适合表单验证,但在我看来,当它可以单独在 Vuetify 中实现时,解决这个问题是多余的。

从@ittus答案继,你需要删除箭头功能passwordConfirmationRule来访问

      return this.password === this.rePassword || "Password must match";
Run Code Online (Sandbox Code Playgroud)

请参阅此代码和框工作示例(现在也使用 Vuetify 2.x)


Ada*_*lov 3

使用 Vee-validate 非常简单:

<div id="app">
  <v-app id="inspire">
    <form>
      <v-text-field
        ref="password"
        type="password"
        v-model="pass"
        v-validate="'required'"
        :error-messages="errors.collect('pass')"
        label="Pass"
        data-vv-name="pass"
        required
      ></v-text-field>
      <v-text-field
        v-model="pass2"
        type="password"
        v-validate="'required|confirmed:password'"
        :error-messages="errors.collect('pass2')"
        label="Pass 2"
        data-vv-name="pass"
        required
      ></v-text-field>

      <v-btn @click="submit">submit</v-btn>
      <v-btn @click="clear">clear</v-btn>
    </form>
  </v-app>
</div>
Run Code Online (Sandbox Code Playgroud)
Vue.use(VeeValidate)

new Vue({
  el: '#app',
  $_veeValidate: {
    validator: 'new'
  },

  data: () => ({
    pass: '',
    pass2: "",
  }),
  methods: {
    submit () {
      this.$validator.validateAll()
        .then(result => {
          console.log(result)
        })
    },
    clear () {
      this.pass = ''
      this.pass2 = ''
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

请记住先安装vee-validate并重新启动本地服务器。

链接到代码笔

链接到文档