How to implement form validation in Vue

How to implement form validation in Vue

1. Installation and use

First, install it in your Vue project:

npm install --save vue-input-check


After the installation is complete, import and register:

import inputCheck from 'vue-input-check';

// Install Vue.use(inputCheck);

Then, we can use it in the form:

<form autocomplete="off" novalidate>
    <input v-model='key' name='Input box name' v-input-check='[key,"validate-express"]'/>
    <!-- You can have as many input boxes as you want-->
</form>


As you can see, v-input-check above is where we define the rules for each input box. The value is an array. The first value is v-model of the input box, and the second value is a string . The syntax is as follows:

validate-express="val1:param1:param2|val2|valu3:param1"


Different rules are separated by |, and the parameters of the rules that need to pass parameters are separated by :. Let’s look at a few examples:

    v-input-check='[key,"required|maxLength:10|regexp:^\\d{1,5}$"]'
    v-input-check='[key,"required"]'


The following built-in rules are currently available:

required:boolean: indicates that the value must be entered. There is an optional parameter indicating whether the value must be entered. The default value is true
maxLength:num: maximum length
minLength:num: minimum length
regexp:str: regular expression

2. Obtaining the Verification Results

After the page rules are defined, you have two ways to get the verification results.

1.JS method

Simply use the following method to start the check:

this.$validateCheck(formnode, callback, errorback);


This object contains three parameters:

  • formnode : the form node that needs to be verified, required
  • callback : Form validity callback, optional
  • errorback : Form illegal callback, optional

In addition, the error callback has a parameter with the data format:

{
    "$el": the wrong input box node "$error": the first error message of the current input box}

2. HTML method

The purpose of providing this method is to provide real-time feedback on the input status of the current form on the page.

First, on the form, you can determine whether the form is valid by judging whether class contains v-valid or v-invalid .

Similarly, where the v-input-check directive is added, you can also determine whether the place is legal. For more specific error details, such as illegal mandatory input, the class will be like v-invalid-required v-invalid .

3. Custom validation rules

In most cases, we may also need to add new validation rules, after all, the default ones are often not enough to meet all business situations:

Vue.use(inputCheck, {

    // Custom validation rules validate: [{

        //Rule name: "XXX",

        // Verification method, return true for legal, false for illegal // Note that, except for el and val, the remaining parameters of this function are passed by ```:``` when used, and there can be any number of them // For example: ``` required:true|phone:parm1:param2 ```
        test: function (el, val, ...) {
            return true|false;
        },

        // Illegal prompt information, should return a string message: function (el, name) {
            return "XXX";
        }
    },
    // There can be multiple validation rules...
    ]

});

This is the end of this article about how to implement form validation in Vue. For more relevant content about implementing form validation in Vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue ElementUI Form form validation
  • Vue Element-ui form validation rule implementation
  • Vue element-ui parent component controls the form validation operation of the child component
  • Vue+element realizes form validation function
  • Using Vee-validate form validation in Vue.js + Nuxt.js project
  • How to use custom instructions to implement form validation in Vue
  • Detailed explanation of the use of Vue2.0 form validation component vee-validate
  • Vue.js form validation plugin

<<:  Troubleshooting MySQL high CPU load issues

>>:  The process of installing and configuring nginx in win10

Recommend

Various types of MySQL indexes

What is an index? An index is a data structure th...

Detailed explanation of the use and precautions of crontab under Linux

Crontab is a command used to set up periodic exec...

SQL query for users who have placed orders for at least seven consecutive days

Create a table create table order(id varchar(10),...

Detailed explanation of setting Context Path in Web application

URL: http://hostname.com/contextPath/servletPath/...

How to use nginx to simulate canary release

This article introduces blue-green deployment and...

Example of how to build a Mysql cluster with docker

Docker basic instructions: Update Packages yum -y...

Quickly master how to get started with Vuex state management in Vue3.0

Vuex is a state management pattern developed spec...

Web page header optimization suggestions

Logo optimization: 1.The logo image should be as ...

A brief introduction to the usage of decimal type in MySQL

The floating-point types supported in MySQL are F...

How to use async await elegantly in JS

Table of contents jQuery's $.ajax The beginni...

How to solve the mysql insert garbled problem

Problem description: When inserting Chinese chara...

MySQL InnoDB monitoring (system layer, database layer)

MySQL InnoDB monitoring (system layer, database l...

Detailed tutorial on installing Python 3.8.1 on Linux

This example takes the installation of Python 3.8...

Flex layout realizes left text overflow and omits right text adaptation

I want to achieve a situation where the width of ...