当前位置:网站首页>Form组件常用校验规则-1(持续更新中~)
Form组件常用校验规则-1(持续更新中~)
2022-07-04 18:45:00 【51CTO】

前言
最近感触越来越深,就是要经常分享,分享自己遇到的坑,分享自己的解决办法,分享自己的各种经验,这非常有必要。因为,我遇到问题第一反应是先看看别人是怎么解决的,肯定有人也有相同的想法,那这个时候就需要大家都非常慷慨地分享。所以,以后我会尽量定期进行总结分享,与人方便与己方便~
参考链接
https://element.eleme.io/#/zh-CN/component/form
https://github.com/yiminghe/async-validator
表单校验简介
使用目的
在防止用户犯错的前提下,尽可能让用户更早地发现并纠正错误。
简单使用
Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item 的 prop 属性设置为需校验的字段名即可。
<
el
-
form :
model
=
"ruleForm" :
rules
=
"rules"
ref
=
"ruleForm"
label
-
width
=
"100px"
class
=
"demo-ruleForm"
>
<
el
-
form
-
item
label
=
"活动名称"
prop
=
"name"
>
<
el
-
input
v
-
model
=
"ruleForm.name"
>
<
/el-input>
<
/el-form-item>
<
el
-
form
-
item
>
<
el
-
button
type
=
"primary"
@
click
=
"submitForm('ruleForm')"
>
立即创建
<
/el-button>
<
el
-
button
@
click
=
"resetForm('ruleForm')"
>
重置
<
/el-button>
<
/el-form-item>
<
/el-form>
<
script
>
export
default {
data() {
return {
ruleForm: {
name:
'',
},
rules: {
name: [
{
required:
true,
message:
'请输入活动名称',
trigger:
'blur' },
{
min:
3,
max:
5,
message:
'长度在 3 到 5 个字符',
trigger:
'blur' }
],
}
};
},
methods: {
submitForm(
formName) {
this.
$refs[
formName].
validate((
valid)
=> {
if (
valid) {
alert(
'submit!');
}
else {
console.
log(
'error submit!!');
return
false;
}
});
},
resetForm(
formName) {
this.
$refs[
formName].
resetFields();
}
}
}
<
/script>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
常用字段
required
是否为必填项,用于验证字段内容是否为空;
type
数据类型,类似数据格式校验,默认是string,常见的类型有number,boolean,integer,float,array,date,email等;
pattern
字段值匹配正则表达式才能通过验证;
min和max
对于string和数组来说,min和max表示最小和最大的长度;对于数字number来说,表示限制值大小;
trigger
触发验证的条件;
validator
可以为指定字段自定义验证函数:function(rule, value, callback);
边栏推荐
- 【深度学习】一文看尽Pytorch之十九种损失函数
- Wireshark network packet capture
- Pytoch learning (4)
- Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- Multi table operation inner join query
- 六石编程学:关于代码,有六个得意
- ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
- ACM组合计数入门
- Utilisation de la barre de progression cbcggprogressdlgctrl utilisée par BCG
猜你喜欢

Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines

实战模拟│JWT 登录认证

Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing

What is involution?

FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet

做社交媒体营销应该注意些什么?Shopline卖家的成功秘笈在这里!

C server log module

针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS

Swagger suddenly went crazy

C # better operation mongodb database
随机推荐
New wizard effect used by BCG
How to adapt your games to different sizes of mobile screen
kotlin 继承
Development and construction of DFI ecological NFT mobile mining system
Swagger suddenly went crazy
Offset function and windowing function
Siemens HMI download prompts lack of panel image solution
What are the consequences of closing the read / write channel?
同事的接口文档我每次看着就头大,毛病多多。。。
Six stones programming: about code, there are six triumphs
被奉为经典的「金字塔原理」,教给我们哪些PPT写作技巧?
[Beijing Xunwei] i.mx6ull development board porting Debian file system
kotlin 循环控制
[QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
Kotlin cycle control
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
Free soldier
Kotlin condition control
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
[QNX hypervisor 2.2 user manual]6.3.1 factory page and control page