There are many ways to write and validate form fields in Vue. This blog introduces three more commonly used validation methods. 1. Write in data for verificationForm Contents <!-- Form --> <el-form ref="rulesForm" :rules="formRules" :model="rulesForm" label-width="200px"> <el-form-item label="User Name:" prop="userName"> <el-input v-model="rulesForm.userName" style="width:300px" maxlength="50"/> </el-form-item> </el-form>
data data() { return { //Omit other data definitions... // Form validation formRules: { userName: [ {required: true,message: "Please enter your username",trigger: "blur"} ] } } }
2. Write inlineForm Contents <!-- Form --> <el-form ref="rulesForm" :rules="formRules" :model="rulesForm" label-width="200px"> <el-form-item label="Bank Name:" prop="accountName" :rules="[{required:true,message:'Please enter the bank name',trigger:'blur'}]"> <el-input v-model="rulesForm.accountName" style="width:300px" maxlength="50"/> </el-form-item> </el-form>
data data has no content 3. Importing externally defined rulesForm Contents <!-- Form --> <el-form ref="rulesForm" :rules="formRules" :model="rulesForm" label-width="200px"> <el-form-item label="Bank card number:" prop="accountNumber"> <el-input v-model="rulesForm.accountNumber" style="width:300px" maxlength="19"/> </el-form-item> </el-form> The form content is consistent with the first method, so I won’t go into details here. Script content <script> // Introducing external validation rules import { validateAccountNumber } from "@/utils/validate"; // Determine whether the bank card account is correct const validatorAccountNumber = (rule, value, callback) => { if (!value) { return callback(new Error("Please enter your account information")); } else { if (validateAccountNumber(value)) { callback(); } else { return callback(new Error('Incorrect account format')) } } }; export default { data() { return { //Omit other data definitions... // Form validation formRules: { accountNumber: [ {required: true,validator: validatorAccountNumber,trigger: "blur"} ] } } } } </script>
validate.js /* Bank Account */ export function validateAccountNumber(str) { const reg = /^([1-9]{1})(\d{14}|\d{18})$/ return reg.test(str) }
The above are all verifications when losing focus. Let's analyze how to verify when the form is submitted. 1. Submit button of the form <!-- Form --> <el-form ref="rulesForm" :rules="formRules" :model="rulesForm" label-width="200px"> <el-form-item> <el-button type="primary" @click="onSubmit('rulesForm')">Save</el-button> <el-button @click="cancel">Cancel</el-button> </el-form-item> </el-form>
2. Methods methods: { // Save onSubmit(formName) { this.$refs[formName].validate(valid => { if (valid) { console.log("success submit!!"); }else{ console.log("error submit!!"); } }); }, // Cancel cancel() { } } this.$refs[formName].validate: formName is the passed 'rulesForm', which is consistent with the rel attribute value of the <el-form> form, so that the form that needs to be validated is specified The complete sample code is as follows 1. rules.vue <template> <div class="app-container"> <el-tabs v-model="activeName"> <el-tab-pane label="Form" name="rulesPane" @tab-click="handleClick"> <!-- Form --> <el-form ref="rulesForm" :rules="formRules" :model="rulesForm" label-width="200px"> <el-form-item label="User Name:" prop="userName"> <el-input v-model="rulesForm.userName" style="width:300px" maxlength="50"/> </el-form-item> <el-form-item label="Bank Name:" prop="accountName" :rules="[{required:true,message:'Please enter the bank name',trigger:'blur'}]"> <el-input v-model="rulesForm.accountName" style="width:300px" maxlength="50"/> </el-form-item> <el-form-item label="Bank card number:" prop="accountNumber"> <el-input v-model="rulesForm.accountNumber" style="width:300px" maxlength="50"/> </el-form-item> <el-form-item> <el-button type="primary" @click="onSubmit('rulesForm')">Save</el-button> <el-button @click="cancel">Cancel</el-button> </el-form-item> </el-form> </el-tab-pane> </el-tabs> </div> </template> <script> import { validateAccountNumber } from "@/utils/validate"; // Determine whether the bank card account is correct const validatorAccountNumber = (rule, value, callback) => { if (!value) { return callback(new Error("Please enter your account information")); } else { if (validateAccountNumber(value)) { callback(); } else { return callback(new Error('Incorrect account format')) } } }; export default { name: "rules", data() { return { activeName: "rulesPane", defaultProps: { children: "children", label: "label" }, rulesForm: { }, // Form validation formRules: { userName: [ { required: true, message: "Please enter your user name", trigger: "blur" } ], accountNumber: [ { required: true, validator: validatorAccountNumber, trigger: "blur" } ], } }; }, created() {}, mounted() {}, methods: { handleClick(tab) { }, // Cancel cancel() { }, // Save onSubmit(formName) { this.$refs[formName].validate(valid => { if (valid) { console.log("success submit!!"); } else { console.log("error submit!!"); return false; } }); } } }; </script> <style lang="scss"> </style> 2. validate.js /* Bank Account */ export function validateAccountNumber(str) { const reg = /^([1-9]{1})(\d{14}|\d{18})$/ return reg.test(str) } Rendering The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to use IDEA to configure tomcat and create JSP files
>>: Detailed explanation of how to create multiple instances of MySQL 5.6 in centos7 environment
Standalone hbase, let’s talk about it first. Inst...
Table of contents What is an event A Simple Examp...
Table of contents What is a trigger Create a trig...
This article shares the specific code for JavaScr...
Table of contents Preface: Encryption algorithm: ...
Table of contents Why use Docker? Docker installa...
Introduction to common Dockerfile instructions in...
Table of contents From father to son: 1. In the s...
Table of contents What is native JavaScript A. Ch...
Table of contents 1. Baidu Map API Access 2. Usin...
Before talking about CSS priority, we need to und...
1. First download from the official website of My...
How to declare a cursor in mysql: 1. Declare vari...
Try installing via pip in a virtual environment: ...
Preface I recently wrote a small demo. Because I ...