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
Use of clip-path polygon The value is composed of...
Core code <!DOCTYPE html> <html lang=&qu...
This article mainly introduces how to add floatin...
In front-end development, we are in direct contac...
Table of contents Preface The need for online XML...
I just learned some html yesterday, and I couldn...
lead Some common triangles on web pages can be dr...
Preface The previous article installed Hadoop, an...
Table of contents Preface How to use Summarize Pr...
At this time, you can use overflow:auto; (when the...
CSS naming conventions (rules) Commonly used CSS ...
Disadvantages of single-node database Large-scale...
Common scenarios of MySQL: getting the intersecti...
The Linux command line provides many commands to ...