對(duì)input表單輸入框檢驗(yàn)重復(fù)值,如對(duì)如下圖參數(shù)名進(jìn)行校驗(yàn)重復(fù)值?
el-form-item添加屬性:rules="rules.paramname"
1.寫一個(gè)rules
rules: {
paramname: [
{ required: true, validator: this.validateTitle, trigger: 'blur' }
],
},
2.Method里面添加如下方法
rule:指的是表單中rules屬性
value:指的表單輸入框中輸入的值文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-504799.html
callback:回調(diào)函數(shù)(再次調(diào)用校驗(yàn)函數(shù))文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-504799.html
//參數(shù)名稱校驗(yàn)
validateTitle (rule, value, callback) {
if (typeof value === 'undefined'||value.length == 0) {
// callback(new Error('請(qǐng)輸入?yún)?shù)'))
this.$message({
message:'【參數(shù)名】不可以為空',
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:'【參數(shù)名:'+value+'】已存在',
type: 'warning'
})
return false
}
}
}
},
到了這里,關(guān)于vue自定義rules,對(duì)input表單輸入框校驗(yàn)重復(fù)值的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!