Home >Web Front-end >Vue.js >About vue using validator: VeeValidate3
## Recommended: "vue.js Tutorial"
Preface
vee-validateThe version is
3.3.7
Installation Tutorial
cnpm install vee-validate --save
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) }) }}
main .js introduce vee-validate.js
import Validate from './vee-validate'Vue.use(Validate)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>
Display
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位的手机号码 ...... }
this.$refs.form.validate()是一个promise,当然你也可以用then
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.
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!