vue 验证两次输入的密码是否一致的方法示例

时间:2022-01-09 21:12:42

好记性不如烂笔头。总也记不住

?
1
2
3
4
5
6
7
8
9
10
11
12
<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个字符之间

关键代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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, value, 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 个字符', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
   ],
   confirmPwd:[
    { required: true, message: '请确认密码', trigger: 'blur' },
    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
    { validator: validatePass2, trigger: 'blur', required: true }
   ],
  }
 }
},

到此这篇关于vue 验证两次输入的密码是否一致的方法示例的文章就介绍到这了,更多相关vue 输入密码验证内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://segmentfault.com/a/1190000025157294