Home> Web Front-end> Vue.js> body text

Detailed explanation of the use of vue validator (vue-validator)

青灯夜游
Release: 2020-11-05 17:57:09
forward
4878 people have browsed it

Detailed explanation of the use of vue validator (vue-validator)

Official document: http://vuejs.github.io/vue-validator/zh-cn/index.html

github project address: https:// github.com/vuejs/vue-validator

See the official documentation for how to use vue-validator alone. This article is used in combination with vue-router.

Install validator

Do not add a custom validator or a public validator that does not need to be used globally, install the validator in main.js , using the CommonJS module specification, you need to explicitly use Vue.use() to install the validator component.

import Validator from 'vue-validator' Vue.use(Validator)
Copy after login

When used together with vue-router, verification must be installed before calling router#map, router#start and other instance methods.

To customize the validator, create a js file and install the validator component in the file. For example: validation.js

import Vue from 'vue' import Validator from 'vue-validator' Vue.use(Validator) //自定义验证器
Copy after login

Custom validator

The official api provided is as follows

input[type="text"] input[type="radio"] input[type="checkbox"] input[type="number"] input[type="password"] input[type="email"] input[type="tel"] input[type="url"] select textarea
Copy after login

But the above does not It must meet our needs. At this time, we need to use another global API for registering and obtaining global validators.

Vue.validator( id, [definition] )
Copy after login

Example Define validation.js The content is as follows

import Vue from 'vue' import Validator from 'vue-validator' Vue.use(Validator) //自定义验证器 //添加一个简单的手机号验证 //匹配0-9之间的数字,并且长度是11位 Vue.validator('tel', function (val) { return /^[0-9]{11}$/.test(val) }); //添加一个密码验证 //匹配6-20位的任何字类字符,包括下划线。与“[A-Za-z0-9_]”等效。 Vue.validator('passw', function (val) { return /^(\w){6,20}$/.test(val) });
Copy after login

Use validator

Validator syntax

  
不得少于3个字符 不得大于15个字符
Copy after login

By default, vue-validator will automatically verify based on the validator and v-validate instructions. However, sometimes we need to turn off automatic verification and trigger verification manually when necessary. If you don't need automatic validation, you can turn off automatic validation through the initial attribute or the v-validate validation rule.

is as follows:

  
不得少于3个字符 不得大于15个字符
Copy after login

Terminal command problem

     
Copy after login

Example: User registration verification

is used A component to display prompt information

toast.vue

  
Copy after login

Registered user: If we need to fill in the mobile phone number and enter the password twice

  
Copy after login

If you click next step, you will be prompted "Please complete the form" because the verification fails; if the text box loses focus after gaining focus, a corresponding error message will be prompted; if the content is filled in correctly, it will prompt that the verification has passed and send a corresponding request.

The effect is as shown

Detailed explanation of the use of vue validator (vue-validator)

Related recommendations:

2020 Summary of front-end vue interview questions (With answers)

vue tutorial recommendation: The latest 5 vue.js video tutorial selections in 2020

More programming-related knowledge, Please visit:Introduction to Programming! !

The above is the detailed content of Detailed explanation of the use of vue validator (vue-validator). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:cnblogs.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