vue3自定義確認密碼匹配驗證規(guī)則的操作代碼
更新時間:2024年01月11日 12:00:00 作者:7.9
這篇文章主要介紹了vue3自定義確認密碼匹配驗證規(guī)則的操作代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
vue3自定義確認密碼匹配驗證規(guī)則

// 自定義確認密碼匹配驗證規(guī)則
const matchPassword = (rules:any, value:any, callback:any) => {
if (value != addData.payPwd) {
callback(new Error('兩次密碼輸入不一致!'))
} else {
callback()
}
}
const rules = reactive({
payPwd: [
{ required: true, message: "請輸入支付密碼", trigger: "blur" },
{
pattern: /^\d+$/,
message: "只能輸入6位數(shù)字",
trigger: "blur",
},
],
repeatedPwd: [
{ required: true, message: "請輸入支付密碼", trigger: "blur" },
{
pattern: /^\d+$/,
message: "只能輸入6位數(shù)字",
trigger: "blur",
},
{validator: matchPassword, trigger: 'blur'}
],
});
<el-dialog
v-model="AddDialog"
title="新增會員"
width="700px"
@close="closeDialog"
>
<el-form
ref="categoryFormRef"
:model="addData"
:rules="rules"
label-width="120px"
>
<el-form-item label="支付密碼" prop="payPwd">
<el-input
v-model="addData.payPwd"
placeholder="請輸入支付密碼"
show-password
maxLength="6"
/>
</el-form-item>
<el-form-item label="確認支付密碼" prop="repeatedPwd">
<el-input
v-model="addData.repeatedPwd"
placeholder="請輸入支付密碼"
show-password
maxLength="6"
/>
</el-form-item>
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button type="primary" @click="handleAdd">確 定</el-button>
<el-button @click="closeDialog">取 消</el-button>
</div>
</template>
</el-dialog>擴展:
vue中表單使用自定義驗證規(guī)則
vue中表單使用自定義驗證規(guī)則(以2次輸入密碼為例)
先安裝elementUI
yarn add element-ui
mian.js中引入elementUI
import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI);
創(chuàng)建表單
<el-form ref="form" :model="form" :rules="rules">
<el-form-item label="新密碼" prop="newPwd">
<el-input type="password" v-model="form.newPwd"></el-input>
</el-form-item>
<el-form-item label="確認密碼" prop="confirmPwd">
<el-input type="password" v-model="form.confirmPwd"></el-input>
</el-form-item>
<el-form-item>
<el-button @click="confirmBtn">確認</el-button>
<el-button @click="resetBtn">重置</el-button>
</el-form-item>
</el-form>定義數(shù)據(jù)及驗證規(guī)則
data() {
var checkPwd = (rule, value, callback) => {
if (value === this.form.newPwd) {
callback();
} else {
callback(new Error('兩次輸入密碼不一致!'));
}
}
return {
form: {
newPwd: '',
confirmPwd: ''
},
rules: {
newPwd: [
{ required: true, message: '請輸入密碼', trigger: 'blur' },
{ min: 6, max: 12, message: '長度在 6 到 12 個字符', trigger: 'blur' }
],
confirmPwd: [
{ required: true, message: '請再次輸入密碼', trigger: 'blur' },
{ min: 6, max: 12, message: '長度在 6 到 12 個字符', trigger: 'blur' },
{ validator: checkPwd, trigger: 'blur' }
]
}
}
},綁定操作按鈕
methods: {
//確認按鈕
confirmBtn() {
this.$refs.form.validate(res => {
if (res) {
console.log('修改成功');
} else {
console.log('修改失敗');
return false;
}
});
},
//重置按鈕
resetBtn() {
this.$refs.form.resetFields();
}
}到此這篇關(guān)于vue3自定義確認密碼匹配驗證規(guī)則的文章就介紹到這了,更多相關(guān)vue3自定義密碼驗證規(guī)則內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ant-design-vue導(dǎo)航菜單a-menu的使用解讀
這篇文章主要介紹了ant-design-vue導(dǎo)航菜單a-menu的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
element tree樹形組件回顯數(shù)據(jù)問題解決
本文主要介紹了element tree樹形組件回顯數(shù)據(jù)問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
vue+element?upload上傳帶參數(shù)的實例
這篇文章主要介紹了vue+element?upload上傳帶參數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue中使用echarts實現(xiàn)繪制人體動態(tài)圖
這篇文章主要為大家詳細介紹了Vue中如何使用echarts實現(xiàn)繪制人體動態(tài)圖,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-03-03

