Vue 范围搜索的两个输入框共用一个验证
一个有一项数据取一个范围,并做验证,但是UI的验证提醒文字是合并在一起的,并不是每个输入框有单独的验证提醒
<template>
<div class="app-container">
<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
<el-form-item label="范围" prop="area">
<el-col :span="11">
<el-form-item prop="start">
<el-input v-model="ruleForm.start" placeholder="请输入起始值(0-1000)" />
</el-form-item>
</el-col>
<el-col class="line" :span="2" style="text-align:center">-</el-col>
<el-col :span="11">
<el-form-item prop="end">
<el-input v-model="ruleForm.end" placeholder="请输入结束值(0-1000)" />
</el-form-item>
</el-col>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data() {
const validateStart = (rule, value, callback) => {
if (!!value) {
// 1-1000的正则
const numReg = /^(?!00)(?:[0-9]{1,3}|1000)$/
if (!numReg.test(value)) {
this.ruleForm.areaRules[0] = 1
// 手动触发最外层form-item的正则验证
this.areaBlur()
return callback()
}
if (numReg.test(value)) {
this.ruleForm.areaRules[0] = 2
this.areaBlur()
return callback()
}
this.ruleForm.areaRules[0] = 0
this.areaBlur()
callback()
} else {
this.ruleForm.areaRules[0] = -1
this.areaBlur()
return callback()
}
}
const validateEnd = (rule, value, callback) => {
if (!!value) {
// 1-1000的正则
const numReg = /^(?!00)(?:[0-9]{1,3}|1000)$/
if (!numReg.test(value)) {
this.ruleForm.areaRules[1] = 1
this.areaBlur()
return callback()
}
if (numReg.test(value)) {
this.ruleForm.areaRules[1] = 2
this.areaBlur()
return callback()
}
this.ruleForm.areaRules[1] = 0
this.areaBlur()
callback()
} else {
this.ruleForm.areaRules[1] = -1
this.areaBlur()
return callback()
}
}
// areaRules内值的含义:-1 提醒不能为空 0则表示正常 其他值为格式错误
const validateArea = (rule, value, callback) => {
if (this.ruleForm.areaRules.includes(-1)) {
return callback(new Error('范围不能为空'))
}
if (this.ruleForm.areaRules[0] !== this.ruleForm.areaRules[1] || this.ruleForm.areaRules[0] === 1 || this.ruleForm.areaRules[1] === 1) {
return callback(new Error('格式错误'))
}
if (this.ruleForm.start > this.ruleForm.end) {
return callback(new Error('起始序号不能大于截止序号'))
}
callback()
}
return {
ruleForm: {
start: '',
end: '',
areaRules: [-1, -1]
},
rules: {
start: [{ type: 'string', required: true, validator: validateStart, trigger: 'blur' }],
end: [{ type: 'string', required: true, validator: validateEnd, trigger: 'blur' }],
area: [{ type: 'array', required: true, validator: validateArea, trigger: 'change' }]
}
}
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!')
} else {
console.log('error submit!!')
return false
}
})
},
// 手动触发最外层form-item的正则验证
areaBlur() {
this.$refs['ruleForm'].validateField('area')
}
}
}
</script>
简单的一种方法如下:
1. 校验prop必写: prop="abscissa"
<el-form-item label="横纵坐标值" prop="abscissa">
<el-input
size="small"
id="nodexLeft"
style="width: 50%"
v-model="form.abscissa"
placeholder="请填写数字"
@input="form.abscissa = $util.onkeyNumber(form.abscissa)"
>
<template slot="prepend">x</template>
</el-input>
<el-input
size="small"
id="nodeyRight"
style="width: 50%"
v-model="form.ordinate"
placeholder="请填写数字"
@input="form.ordinate = $util.onkeyNumber(form.ordinate)"
>
<template slot="prepend">y</template>
</el-input>
</el-form-item>
2. data下添加自定义校验规则 + 普通定义校验rules
data() {
// 自定义横纵坐标验证规则
const checknodexy = (rule, value, callback) => {
if (!(this.form.ordinate && this.form.abscissa)) {
return callback(new Error('请填入横纵坐标'));
} else {
callback();
}
};
return {
rules: {
abscissa: [
// checknodexy用的是上面定义的
{ validator: checknodexy, required: true, trigger: 'blur' }
]
},
}
}
作者:Carver-大脸猫
出处:https://www.cnblogs.com/carver/articles/18209557
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
转载请注明原处
本文来自博客园,作者:Carver-大脸猫,转载请注明原文链接:https://www.cnblogs.com/carver/articles/18209557
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现