vue 驗證兩次輸入的密碼是否一致的方法示例
阿新 • • 發佈:2020-10-03
好記性不如爛筆頭。總也記不住
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"> <el-form-item label="新密碼" prop="newPwd"> <el-input v-model="ruleForm.newPwd" clearable size="small" type="password"></el-input> </el-form-item> <el-form-item label="確認密碼" prop="confirmPwd"> <el-input v-model="ruleForm.confirmPwd" clearable size="small" type="password"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('ruleForm')" class="button">確定</el-button> <el-button @click="resetForm('ruleForm')" class="button">重置</el-button> </el-form-item> </el-form>
驗證兩次密碼是否一致 卻保證輸入的密碼在6-16個字元之間
關鍵程式碼如下:
data() { var validatePass = (rule,value,callback) => { if (value === '') { callback(new Error('請輸入密碼')); } else { if (this.ruleForm.confirmPwd !== '') { this.$refs.ruleForm.validateField('confirmPwd'); } callback(); } }; var validatePass2 = (rule,callback) => { if (value === '') { callback(new Error('請再次輸入密碼')); } else if (value !== this.ruleForm.newPwd) { callback(new Error('兩次輸入密碼不一致!')); } else { callback(); } }; return { changePwdDialog:false,//修改密碼彈框 ruleForm: { newPwd: '',confirmPwd:'' },rules: { newPwd: [ { required: true,message: '請輸入密碼',trigger: 'blur' },{ min: 6,max: 16,message: '長度在 6 到 16 個字元',{ validator: validatePass,trigger: 'blur' } ],confirmPwd:[ { required: true,message: '請確認密碼',{ validator: validatePass2,trigger: 'blur',required: true } ],} } },
到此這篇關於vue 驗證兩次輸入的密碼是否一致的方法示例的文章就介紹到這了,更多相關vue 輸入密碼驗證內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!