Vue中 修改密碼與重復密碼的校驗


<el-dialog title="修改登錄密碼" :visible.sync="newPwdVisible" center>
  <el-form :model="newForm" ref="newForm" :rules="ruleValidate">
     <el-form-item label="新密碼" prop="newPwd">
        <el-input v-model="newForm.newPwd" type="password" clearable placeholder="請輸入6-16位數字、字母(區分大小寫)、符號的組合"></el-input>
     </el-form-item>
     <el-form-item label="再次輸入新密碼" prop="newAgainPwd">
        <el-input v-model="newForm.newAgainPwd" type="password" clearable placeholder="請再次輸入新密碼"></el-input>
     </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
     <el-button @click="newPwdVisible = false">取 消</el-button>
     <el-button type="primary" @click="saveInfo">確 定</el-button>
  </div>
</el-dialog>


data () {
  
      // 新密碼校驗
      const pwdCheck = async(rule, value, callback) => {
     // const reg = /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{6,16}$/;
     const reg = /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,16}$/;
      if (value !== '') {
        if (value.length < 6) {
          return callback(new Error('新密碼至少輸入6位'))
        } else if (value.length > 16) {
          return callback(new Error('密碼最長不能超過16位'))
        } else if (!reg.test(value)) {
          return callback(new Error('密碼輸入有誤,請檢查格式是否正確!'))
        } else {
          callback()
        }
      }else{
        return callback(new Error('新密碼不能為空'))
       }
     }
     // 重復密碼驗證
    const pwdAgainCheck = async(rule, value, callback) => {
      if (value.length < 1) {
        return callback(new Error('重復密碼不能為空!'));
      } else if(this.newForm.newPwd !== this.newForm.newAgainPwd){
        return callback(new Error('兩次輸入密碼不一致!'));
     }else{
       callback()
     }
   }
  return {
newForm: { 
newPwd: '',
newAgainPwd: ''
},
newPwdVisible: false,
ruleValidate: {
      newPwd: [ { required: true, validator: pwdCheck, trigger: 'blur' } ],
      newAgainPwd: [ { required: true, validator: pwdAgainCheck, trigger: 'blur' } ]
}
  }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM