
vue自定义rules,对input表单输入框校验重复值
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/vue

·
对input表单输入框检验重复值,如对如下图参数名进行校验重复值
el-form-item添加属性:rules="rules.paramname"
1.写一个rules
rules: {
paramname: [
{ required: true, validator: this.validateTitle, trigger: 'blur' }
],
},
2.Method里面添加如下方法
rule:指的是表单中rules属性
value:指的表单输入框中输入的值
callback:回调函数(再次调用校验函数)
//参数名称校验
validateTitle (rule, value, callback) {
if (typeof value === 'undefined'||value.length == 0) {
// callback(new Error('请输入参数'))
this.$message({
message:'【参数名】不可以为空',
type: 'warning'
})
} else {
try {
let x = 0;
for(let i = 0; i < this.myform.userList.length; i++ ){
if (this.myform.userList[i].name == value){
x=x+1;
}
if(x == 2){
throw new Error("type-check-error");
}
}
callback()
return true
} catch (e) {
if (e.message == "type-check-error") {
this.$message({
message:'【参数名:'+value+'】已存在',
type: 'warning'
})
return false
}
}
}
},




vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:7 个月前 )
9e887079
[skip ci] 5 个月前
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 9 个月前
更多推荐
所有评论(0)