当前位置:网站首页>自定义form表单验证
自定义form表单验证
2022-08-03 19:46:00 【叶赫那拉 赫敏】
什么是表单验证?
表单验证通常往往用于一些信息填写完成以后, 前端提交到服务器之前的一个自检过程,比如:用户输入的手机号是都正确?用户输入的邮箱是否正确?此项信息是否是必填?
在提交到服务器之前去拦截信息的正确性。正确则提交服务器,错误则弹出警告提示!
基础的表单校验(Element-UI form):
**email **: 校验邮箱格式
maxlength:最大输入长度
minlength:最小输入长度
为什么要自定义form表单验证?
当使用form表单时,基础的一些验证满足不了我们项目中的需求时,这时候我们就需要去自定义验证规则。
如何自定义form表单验证?
现在我们来定义一个在项目中使用过的表单验证,来校验我们输入的富文本内容是否为空,包括空格。
Html部分
01
<el-form label-position="right"
:rules="rules"
size="small"
ref="formRef"
:model="form">
<el-form-item prop="content">
<Editor v-if="isnextTick"
:content="content"
:editorRef="editor2"
@keydown="(e) => keydownup(e)"
@onChangeEdito="onChangeEdito">
</Editor>
</el-form-item>
定义Rules部分
02
const rules = reactive({
content: [{ required: true, validator: checkSpace, trigger: "blur" }]
});
const checkSpace = (rule, value, callback) => {
if (!value.trim()) {
callback(new Error("发布评论不能为空"));
} else {
callback();
}
};
const checkVal = (str) => {
let num = 0;
const reg = /<p>(&nbsp;|&nbsp;\s+)+<\/p>|<p>(<br\/>)+<\/p>/g;
while (num < str.length && str !== "") {
num++;
const k = str.match(reg);
if (k) {
str = str.replace(k[0], "");
}
}
return str === "";
};
form表单校验根据自己的需求不同,可以自己定义校验规则,从而更灵活的满足需求。
边栏推荐
猜你喜欢

Detailed demonstration pytorch framework implementations old photo repair (GPU)
手把手教你定位线上MySQL慢查询问题,包教包会

揭秘5名运维如何轻松管理数亿级流量系统
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting

Detailed AST abstract syntax tree

阿里巴巴政委体系-第八章、阿里政委工作方法论

边缘盒子+时序数据库,美的数字化平台 iBuilding 背后的技术选型

149. The largest number on a straight line, and check the set

Matlab论文插图绘制模板第42期—气泡矩阵图(相关系数矩阵图)

阿里巴巴政委体系-第六章、阿里政委体系运作
随机推荐
JWT详解
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
X86 function call model analysis
剑指 Offer II 044. 二叉树每层的最大值-dfs法
Use ControlTemplate or Style from resource file in WPF .cs and find the control
CS免杀姿势
The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
C中的数据存储
利用net-snmp的库实现snmpget,snmpset
149. 直线上最多的点数-并查集做法
ADS 2023 Download Link
力扣刷题之爬楼梯(7/30)
ADS 2023 下载链接
Postgresql source code (65) analysis of the working principle of the new snapshot system Globalvis
微导纳米IPO过会:年营收4.28亿 君联与高瓴是股东
MySQL master-slave, 6 minutes you master!
NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
不要再用if-else
Calculation of the array serial number of Likou brush questions (one question per day 7/28)
Unity gets the actual coordinates of the ui on the screen under the canvas