当前位置:网站首页>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
边栏推荐
- Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示
- 记录:调用mapper报空指针;<foreach>不去重的用法;
- JDBC tool class jdbcconutil gets the connection to the database
- 使用 WPAD/PAC 和 JScript在win11中进行远程代码执行1
- The market share of the financial industry exceeds 50%, and zdns has built a solid foundation for the financial technology network
- Numpy mathematical function & logical function
- Mysql database - connection query
- 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
- 16MySQL之DCL 中 COMMIT和ROllBACK
- R语言使用econocharts包创建微观经济或宏观经济图、indifference函数可视化无差异曲线、自定义计算交叉点、自定义配置indifference函数的参数丰富可视化效果
猜你喜欢
aqs的学习
Compact CUDA tutorial - CUDA driver API
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
[target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
STM32 Basics
Openharmony open source developer growth plan, looking for new open source forces that change the world!
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
After route link navigation, the sub page does not display the navigation style problem
Linux64Bit下安装MySQL5.6-不能修改root密码
随机推荐
Design of warehouse management database system
Remote code execution in Win 11 using wpad / PAC and JScript 1
Introduction to electron tutorial 3 - process communication
Building googlenet neural network based on pytorch for flower recognition
程序设计语言基础(2)
R语言使用timeROC包计算无竞争风险情况下的生存资料多时间AUC值、使用confint函数计算无竞争风险情况下的生存资料多时间AUC指标的置信区间值
NC basic usage
Intersection calculation of straight line and plane in PCL point cloud processing (53)
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
Es index (document name) fuzzy query method (database name fuzzy query method)
Alicloud: could not connect to SMTP host: SMTP 163.com, port: 25
论文写作 19: 会议论文与期刊论文的区别
STM32 Basics
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
How does onlyoffice solve no route to host
Research on open source OCR engine
The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
Handwritten Google's first generation distributed computing framework MapReduce
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning