当前位置:网站首页>Common form verification
Common form verification
2022-04-23 20:19:00 【wytraining】
form Verification of forms
One : Common form verification
1.data
// Unit information
info: {},
// Form validation rules
formValidators: {
name: [{ required: true, message: ' Please enter the company name ' }],
phone: [{ validator: this.validatePhone }],
email: [{ validator: this.validateEmail }],
},
2.methods
// Submit
submit () {
this.$refs.drawerForm.validate((valid) => {
if (valid) {
if (!this.externalId) {
this.addExternal() // newly added
} else {
this.editExternal() // edit
}
}
})
},
// Check phone
validatePhone (rule, value, callback) {
if (value) {
const tel = /^0\d{2,3}-?\d{7,8}$/
const phone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/
if (value.length === 11) { // Phone number
if (phone.test(value)) {
callback()
}
} else if (value.length === 13 && value.indexOf('-') !== -1) { // Phone number
if (tel.test(value)) {
callback()
}
}
callback(new Error(' Please enter the correct phone number '))
} else {
callback()
}
},
// Checkbox
validateEmail (rule, value, callback) {
if (value) {
const email = /^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/
if (email.test(value)) {
callback()
} else {
callback(new Error(' Please enter the correct email '))
}
} else {
callback()
}
},
3.template
<Form
ref='drawerForm'
:label-width='80'
:rules='formValidators'
:model='info'
>
<FormItem
label=' Name of the company :'
prop='name'
>
<Input
v-model.trim='info.name'
clearable
placeholder=' Please enter the company name '
maxlength='16'
/>
</FormItem>
<FormItem
label=' contact number :'
prop='phone'
>
<Input
v-model.trim='info.phone'
clearable
placeholder=' Please enter the contact number '
/>
</FormItem>
<FormItem
label=' mailbox :'
prop='email'
>
<Input
v-model.trim='info.email'
clearable
placeholder=' Please enter email address '
/>
</FormItem>
<FormItem
label=' remarks :'
>
<Input
v-model.trim='info.description'
clearable
type='textarea'
placeholder=' Please enter comments '
:rows='5'
:show-word-limit='true'
:autosize='false'
:maxlength='100'
/>
</FormItem>
</Form>
Two :v-if The form verification in does not take effect ?
1. Problem description
In use form When checking the form , Because some fields are displayed and hidden according to special conditions , So use the v-if Control display ; But it was found that v-if Control form elements cannot always be verified .
2. resolvent
Set v-if Plus the elements of key value
<template v-if="result===1">
<FormItem label=' Hidden danger name :' prop='trouble_name' key='trouble_name' >
<Input v-model.trim='info.trouble_name' placeholder=' Please enter the hidden danger name ' clearable />
</FormItem>
</template>
3. reason
1. Use v-if: Yes form The form contains prop Property to verify rule binding , Is in vue Declaration period mounted Accomplished . and v-if The elements used to switch are destroyed , Led to v-if Form items in , Because in mounted The period is not rendered , So the rules are not bound , Therefore, rules that do not meet the display conditions during initialization will not be generated , Causes the following switching conditions , The verification of the displayed input box will not take effect .
2. Use v-show: All the rules are generated during initialization , Even if it is hidden, it will check the rules .
版权声明
本文为[wytraining]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210552098339.html
边栏推荐
- How to do product innovation—— Exploration of product innovation methodology I
- Leetcode dynamic planning training camp (1-5 days)
- [2022] regard 3D target detection as sequence prediction - point2seq: detecting 3D objects as sequences
- Understanding various team patterns in scrum patterns
- LeetCode动态规划训练营(1~5天)
- Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
- Five minutes to show you what JWT is
- 論文寫作 19: 會議論文與期刊論文的區別
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
猜你喜欢

Leetcode dynamic planning training camp (1-5 days)

山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(八)

Mysql database backup scheme

【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码

Compact CUDA tutorial - CUDA driver API
![Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]](/img/1a/669c330e64af8e75f4b05e472d03d3.png)
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]

Fundamentals of programming language (2)

SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions

MySQL advanced lock - overview of MySQL locks and classification of MySQL locks: global lock (data backup), table level lock (table shared read lock, table exclusive write lock, metadata lock and inte

WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
随机推荐
Design of warehouse management database system
Es index (document name) fuzzy query method (database name fuzzy query method)
VeraCrypt文件硬盘加密使用教程
使用 WPAD/PAC 和 JScript在win11中进行远程代码执行1
NC basic usage 3
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
The R language uses the timeroc package to calculate the multi time AUC value of survival data without competitive risk, and uses the confint function to calculate the confidence interval value of mul
Alicloud: could not connect to SMTP host: SMTP 163.com, port: 25
Introduction to electron Tutorial 4 - switching application topics
Notes of Tang Shu's grammar class in postgraduate entrance examination English
[2022] regard 3D target detection as sequence prediction - point2seq: detecting 3D objects as sequences
Recommend an open source free drawing software draw IO exportable vector graph
Why does ES6 need to introduce map when JS already has object type
中金财富公司怎么样,开户安全吗
Sqoop imports data from Mysql to HDFS using lzop compression format and reports NullPointerException
NC basic usage 2
Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
Change the material of unity model as a whole
NC basic usage 1
Fundamentals of programming language (2)