Home > Web Front-end > Vue.js > About vue using validator: VeeValidate3

About vue using validator: VeeValidate3

藏色散人
Release: 2020-09-19 16:09:57
forward
3604 people have browsed it

About vue using validator: VeeValidate3

## Recommended: "vue.js Tutorial"

Preface

I am using

vee-validateThe version is 3.3.7

Installation Tutorial

cnpm install vee-validate --save
Copy after login

New file vee-validate.js

import { extend, ValidationObserver, ValidationProvider, localize } from 'vee-validate'import { required, email, min, between, numeric } from 'vee-validate/dist/rules'import zh_CN from 'vee-validate/dist/locale/zh_CN.json';localize('zh_cn',zh_CN) // 配置中文export default {
  install (Vue) {
    Vue.component('ValidationObserver', ValidationObserver)
    Vue.component('ValidationProvider', ValidationProvider)
    extend('required', required)
    extend('email', email)
    extend('min', min)
    extend('between', between)
    extend('numeric', numeric)
    extend('phone', {
      message: '请输入11位的手机号码',
      validate: value => value.length === 11 && /^1[3456789]\d{9}$/.test(value)
    })
  }}
Copy after login

main .js introduce vee-validate.js

import Validate from './vee-validate'Vue.use(Validate)
Copy after login
The installation has been completed

Usage tutorial

New form

<template>

 <ValidationObserver ref="form">

 <form @submit.prevent="handleAddAddress">

  <!-- 要验证的输入框 -->
 <ValidationProvider rules="required|phone" v-slot="{ errors }" name="手机号码">
  <input type="text" v-model="phone" />
     {{ errors[0] }} <!-- 错误信息显示 -->
 </ValidationProvider><button type="submit">确认</button>
 </form>

 </ValidationObserver></template>
Copy after login

Display

vue使用验证器: VeeValidate3
vue使用验证器: VeeValidate3

method processing

 methods:{

      async checkValid () { // 是否验证通过
        return this.$refs.form.validate();
      },
      async handleAddAddress(e){
        let check = await this.checkValid();
        if(! check) // 验证不通过
        {
          let errors = Object.values(this.$refs.form.errors);
          let first_error_index = errors.findIndex(val => val.length >= 1);
          alert(errors[first_error_index][0])
        }    
        // 弹出请输入11位的手机号码

        ......
    }
Copy after login

vue使用验证器: VeeValidate3

this.$refs.form.validate()是一个promise,当然你也可以用then
Copy after login

Tucao

2It’s much simpler, I just want to do a simple verification.

3 was inspired by lar. Personally, I find it really painful and super troublesome.

Each

input must be wrapped by ValidationProvider to be verified. (
input should be encapsulated into a custom component)                                                    

The above is the detailed content of About vue using validator: VeeValidate3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template