当前位置:网站首页>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);
边栏推荐
- Anhui Zhong'an online culture and tourism channel launched a series of financial media products of "follow the small editor to visit Anhui"
- How is the entered query SQL statement executed?
- 输入的查询SQL语句,是如何执行的?
- Swagger suddenly went crazy
- 同事的接口文档我每次看着就头大,毛病多多。。。
- @Data source connection pool exhaustion caused by transactional abuse
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- Template_ Large integer subtraction_ Regardless of size
- Huawei cloud store homepage banner resource bit application
- 记一次 .NET 某工控数据采集平台 线程数 爆高分析
猜你喜欢

Dark horse programmer - software testing - stage 08 2-linux and database-23-30-process port related, modify file permissions, obtain port number information, program and process related operations, Li

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

#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏

多表操作-内连接查询

The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!

华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙

QT writing the Internet of things management platform 38- multiple database support

C # use stopwatch to measure the running time of the program

Introduction to ACM combination counting

Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
随机推荐
六石编程学:关于代码,有六个得意
Key rendering paths for performance optimization
[QNX hypervisor 2.2 user manual]6.3.1 factory page and control page
HMM hidden Markov model and code implementation
Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
New wizard effect used by BCG
MySQL中的日期时间类型与格式化方式
AP8022开关电源小家电ACDC芯片离线式开关电源IC
软件客户端数字签名一定要申请代码签名证书吗?
黑马程序员-软件测试--07阶段2-linux和数据库-09-24-linux命令学习步骤,通配符,绝对路径,相对路径,文件和目录常用命令,文件内容相关操作,查看日志文件,ping命令使用,
Chrome development tool: what the hell is vmxxx file
Six stones programming: about code, there are six triumphs
Niuke Xiaobai month race 7 who is the divine Archer
Pytoch learning (4)
Thinking on demand development
C language - Introduction - Foundation - grammar - process control (VII)
1008 elevator (20 points) (PAT class a)
Informatics Olympiad 1336: [example 3-1] find roots and children
QT writing the Internet of things management platform 38- multiple database support
记一次 .NET 某工控数据采集平台 线程数 爆高分析