vue3如何实现​6位支付密码输入框

 更新时间:2023年04月27日 15:46:58   作者:前端爱码狮  
微信、支付宝支付密码时的密码输入框大家都很熟悉,本文主要介绍了vue3如何实现​6位支付密码输入框,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

今天要和大家分享的是关于如何实现6位支付密码输入框组件。

因为在web端不像移动端那样,它没有成熟的、已封装好的6位支付密码输入框UI组件,所以需要我们自己来进行处理,从布局、样式、功能等方面进行实现,在此做一下记录。

具体的需求: 在客户信息表格的操作栏中,点击修改支付密码按钮,会跳转到6位支付密码输入框组件页面。同时,要求输入框密文显示、不可编辑、不可回退、即时显示;到达6位数,自动进入确认支付密码;确认支付密码到达6位数,自动检验两次输入密码的一致性,显示确定按钮。此功能是为了用于在银行中,客户用设备输入密码,柜员不可见密码,但柜员可以进行提示操作。

具体的问题: 1、如何实现密文显示,且每个框只能输入1位数字;2、如何实现输入框不可编辑、不可回退;3、如何检验两次输入密码的一致性;4、如果自己的业务需要对键盘按键做限制,该怎么处理。

今天,我们就在这篇文章,用最简洁的语言,来好好地理理上述问题。

一、代码总览

实现6位支付密码输入框组件的代码如下,复制即可直接使用!

<template>
  <div style="display: flex; flex-direction: column">
    <!-- 密码输入框 -->
    <div class="input-box" style="margin: auto">
      <!-- 输入密码 -->
      <div style="font-size: 20px; margin-bottom: 5px">{{ "输入密码" }}</div>
      <div class="input-content" @keyup="keyup" @input="inputEvent">
        <input max="9" min="0" maxlength="1" data-index="0" v-model.number="state.input[0]" type="password"
          ref="firstinput" :disabled="state.disabledInput[0]" />
        <input max="9" min="0" maxlength="1" data-index="1" v-model.number="state.input[1]" type="password"
          :disabled="state.disabledInput[1]" />
        <input max="9" min="0" maxlength="1" data-index="2" v-model.number="state.input[2]" type="password"
          :disabled="state.disabledInput[2]" />
        <input max="9" min="0" maxlength="1" data-index="3" v-model.number="state.input[3]" type="password"
          :disabled="state.disabledInput[3]" />
        <input max="9" min="0" maxlength="1" data-index="4" v-model.number="state.input[4]" type="password"
          :disabled="state.disabledInput[4]" />
        <input max="9" min="0" maxlength="1" data-index="5" v-model.number="state.input[5]" type="password"
          :disabled="state.disabledInput[5]" />
      </div>
      <!-- 确认密码 -->
      <div style="margin-top: 30px; font-size: 20px; margin-bottom: 5px">{{ "确认密码" }}</div>
      <div class="input-content" @keyup="confirmKeyUp" @input="confirmInputEvent">
        <input max="9" min="0" maxlength="1" data-index="0" v-model.number="state.confirmInput[0]" type="password"
          ref="confirmfirstinput" :disabled="state.disabledConfirmInput[0]" />
        <input max="9" min="0" maxlength="1" data-index="1" v-model.number="state.confirmInput[1]" type="password"
          :disabled="state.disabledConfirmInput[1]" />
        <input max="9" min="0" maxlength="1" data-index="2" v-model.number="state.confirmInput[2]" type="password"
          :disabled="state.disabledConfirmInput[2]" />
        <input max="9" min="0" maxlength="1" data-index="3" v-model.number="state.confirmInput[3]" type="password"
          :disabled="state.disabledConfirmInput[3]" />
        <input max="9" min="0" maxlength="1" data-index="4" v-model.number="state.confirmInput[4]" type="password"
          :disabled="state.disabledConfirmInput[4]" />
        <input max="9" min="0" maxlength="1" data-index="5" v-model.number="state.confirmInput[5]" type="password"
          :disabled="state.disabledConfirmInput[5]" />
      </div>
    </div>
    <!-- 按钮 -->
    <div style="margin: auto; margin-top: 30px">
      <el-button type="info" :disabled="state.disabledConfirm" @click="reConfirm"
        :class="[state.disabledConfirm ? 'noActive' : 'active']">{{ "确定" }}</el-button>
      <el-button type="warning" @click="reset">{{ "重新输入" }}</el-button>
    </div>
    <!-- 提示区 -->
    <div
      style="width: 500px; height: 200px; padding: 10px; border: 1px solid #ccc; margin: auto; margin-top: 30px; color: red; text-align: center; font-size: 24px">
      <p>{{ state.tipContent }}</p>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { nextTick, reactive, ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus'
const state = reactive({
  // 输入数组
  input: ["", "", "", "", "", ""],
  // 确认输入数组
  confirmInput: ["", "", "", "", "", ""],
  // 存放粘贴进来的数字
  pasteResult: [],
  confirmPasteResult: [],
  // 一上来禁用确定按钮
  disabledConfirm: true,
  // 输入框是否禁用
  disabledInput: [false, false, false, false, false, false],
  disabledConfirmInput: [false, false, false, false, false, false],
  // 提示内容
  tipContent: "请告知客户输入6位数字密码,输入完毕后,点击回车确认。"
})
// 获取第一个元素的ref
const firstinput = ref()
const confirmfirstinput = ref()
// 页面一加载就使第一个框聚焦
onMounted(() => {
  // 等待dom渲染完成,在执行focus,否则无法获取到焦点
  nextTick(() => {
    firstinput.value.focus();
  });
})
// @input的处理方法
// 解决一个输入框输入多个字符
const inputEvent = (e) => {
  var index = e.target.dataset.index * 1;
  var el = e.target;
  // 限制只能输入数字
  el.value = el.value.replace(/[^\d]/g, "");
  if (el.value.length >= 1) {
    // 密文显示、不可编辑、不可回退、即时显示
    state.disabledInput[index] = true;
    if (el.nextElementSibling) {
      el.nextElementSibling.focus();
    }
  }
  // 到达6位数,自动进入确认支付密码
  if (!el.nextElementSibling) {
    confirmfirstinput.value.focus();
    state.tipContent = "请告知客户再次输入6位数字密码,输入完毕后,点击回车确认。";
  }
}
// @keydown的处理方法,根据业务需要添加
// 此示例没有使用
const keydown = (e) => {
  var index = e.target.dataset.index * 1;
  var el = e.target;
  // 回退键
  if (e.key === 'Backspace') {
    if (state.input[index].length > 0) {
      state.input[index] = ''
    } else {
      if (el.previousElementSibling) {
        el.previousElementSibling.focus()
        state.input[index - 1] = ''
      }
    }
  }
  // 删除键 
  else if (e.key === 'Delete') {
    if (state.input[index].length > 0) {
      state.input[index] = ''
    } else {
      if (el.nextElementSibling) {
        state.input[1] = ''
      }
    }
    if (el.nextElementSibling) {
      el.nextElementSibling.focus()
    }
  }
  // 左键
  else if (e.key === 'ArrowLeft') {
    if (el.previousElementSibling) {
      el.previousElementSibling.focus()
    }
  }
  // 右键 
  else if (e.key === 'ArrowRight') {
    if (el.nextElementSibling) {
      el.nextElementSibling.focus()
    }
  }
  // 上键 
  else if (e.key === 'ArrowUp') {
    if (Number(state.input[index]) * 1 < 9) {
      state.input[index] = (Number(state.input[index]) * 1 + 1).toString()
    }
  }
  // 下键  
  else if (e.key === 'ArrowDown') {
    if (Number(state.input[index]) * 1 > 0) {
      state.input[index] = (Number(state.input[index]) * 1 - 1).toString()
    }
  }
}
// @keyup的处理方法
const keyup = (e) => {
  var index = e.target.dataset.index * 1;
  // 如果为最后一个框,则输入框全部失焦
  if (index === 5) {
    if (state.input.join("").length === 6) {
      document.activeElement.blur();
    }
  }
}
// @input的处理方法
// 解决一个输入框输入多个字符
const confirmInputEvent = (e) => {
  var index = e.target.dataset.index * 1;
  var el = e.target;
  if (el.value.length >= 1) {
    // 密文显示、不可编辑、不可回退、即时显示
    state.disabledConfirmInput[index] = true;
    if (el.nextElementSibling) {
      el.nextElementSibling.focus();
    }
  }
  // 到达6位数,自动检验两次输入密码的一致性
  if (!el.nextElementSibling) {
    // 一一比较元素值,有一个不相等就不等
    for (let i = 0; i < state.input.length; i++) {
      if (state.input[i] !== state.confirmInput[i]) {
        state.tipContent = "请告知客户两次密码输入不一致,柜员点击重新输入,清空密码后请告知客户重新输入。";
        return;
      }
    }
    state.tipContent = "密码合规,点击确定按钮进行修改。";
    // 确定按钮变为可用
    state.disabledConfirm = false;
  }
}
// @keydown的处理方法,根据业务需要添加
// 此示例没有使用
const confirmKeydown = (e) => {
  var index = e.target.dataset.index * 1;
  var el = e.target;
  // 回退键
  if (e.key === 'Backspace') {
    if (state.confirmInput[index].length > 0) {
      state.confirmInput[index] = ''
    } else {
      if (el.previousElementSibling) {
        el.previousElementSibling.focus()
        state.confirmInput[index - 1] = ''
      }
    }
  }
  // 删除键 
  else if (e.key === 'Delete') {
    if (state.confirmInput[index].length > 0) {
      state.confirmInput[index] = ''
    } else {
      if (el.nextElementSibling) {
        state.confirmInput[1] = ''
      }
    }
    if (el.nextElementSibling) {
      el.nextElementSibling.focus()
    }
  }
  // 左键
  else if (e.key === 'ArrowLeft') {
    if (el.previousElementSibling) {
      el.previousElementSibling.focus()
    }
  }
  // 右键 
  else if (e.key === 'ArrowRight') {
    if (el.nextElementSibling) {
      el.nextElementSibling.focus()
    }
  }
  // 上键 
  else if (e.key === 'ArrowUp') {
    if (Number(state.confirmInput[index]) * 1 < 9) {
      state.confirmInput[index] = (Number(state.confirmInput[index]) * 1 + 1).toString()
    }
  }
  // 下键  
  else if (e.key === 'ArrowDown') {
    if (Number(state.confirmInput[index]) * 1 > 0) {
      state.confirmInput[index] = (Number(state.confirmInput[index]) * 1 - 1).toString()
    }
  }
}
// @keyup的处理方法
const confirmKeyUp = (e) => {
  var index = e.target.dataset.index * 1;
  // 如果为最后一个框,则输入框全部失焦
  if (index === 5) {
    if (state.confirmInput.join("").length === 6) {
      document.activeElement.blur();
    }
  }
}
// 重新输入
const reset = () => {
  state.disabledConfirm = true;
  state.tipContent = "请告知客户输入6位数字密码,输入完毕后,点击回车确认。";
  state.input = ["", "", "", "", "", ""];
  state.confirmInput = ["", "", "", "", "", ""];
  state.disabledInput = [false, false, false, false, false, false];
  state.disabledConfirmInput = [false, false, false, false, false, false];
  // 等待dom渲染完成,在执行focus,否则无法获取到焦点
  nextTick(() => {
    firstinput.value.focus();
  });
}
// 确认修改
const reConfirm = () => {
  ElMessageBox.confirm(
    '是否确定修改?',
    '温馨提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      // 此处调修改支付密码接口
      ElMessage({
        type: 'success',
        message: '修改成功!',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消修改!',
      })
    })
}
</script>
<style lang="scss" scoped>
.input-box {
  .input-content {
    width: 512px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    input {
      color: inherit;
      font-family: inherit;
      border: 0;
      outline: 0;
      border-bottom: 1px solid #919191;
      height: 60px;
      width: 60px;
      font-size: 44px;
      text-align: center;
    }
  }
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    appearance: none;
    margin: 0;
  }
}
.noActive {
  color: #fff !important;
  border-width: 0px !important;
  background-color: #ccc !important;
}
.active {
  color: #fff !important;
  border-width: 0px !important;
  background-color: #67c23a !important;
}
</style>
复制代码

二、问题解析

1、问:如何实现密文显示,且每个框只能输入1位数字?

答: 对于实现密文显示,将输入框的类型type改成password即可。对于实现每个框只能输入1位数字,这里只使用输入框的maxlength属性效果并不完美,可能会出现限制不住的情况,需要在@input事件中,判断当前元素值的长度,如果大于等于1,则通过nextElementSibling.focus(),让光标聚焦到下一个兄弟元素上去。

2、问:如何实现输入框不可编辑、不可回退?

答:使用了输入框的disabled属性,通过在@input事件中,将当前输入元素的disabled属性变为true即可。这里把输入框的disabled属性值,分别存在了一个数组中,为了方便后续的获取修改。

3、问:如何检验两次输入密码的一致性?

答:使用了最简单的for循环,遍历输入密码数组和确认密码数组,一一比较它们的元素值,有一个不相等就不等,通过return;结束整个函数的执行。

4、问:如果自己的业务需要对键盘按键做限制,该怎么处理?

答:可以为输入框添加@keydown或@keyup事件,在回调内部通过对key做判断,来对不同的按键做一些业务的处理。

到此这篇关于vue3如何实现​6位支付密码输入框的文章就介绍到这了,更多相关vue3 支付密码输入框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue实现购物车的全选、单选、显示商品价格代码实例

    Vue实现购物车的全选、单选、显示商品价格代码实例

    这篇文章主要介绍了Vue实现购物车的全选、单选、显示商品价格实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • vue移动端下拉刷新和上滑加载

    vue移动端下拉刷新和上滑加载

    这篇文章主要为大家详细介绍了vue移动端下拉刷新和上滑加载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • vue 数据双向绑定的实现方法

    vue 数据双向绑定的实现方法

    这篇文章主要介绍了vue 数据双向绑定的实现方法,帮助大家更好的理解和学习使用vue框架,感兴趣的朋友可以了解下
    2021-03-03
  • vue项目中做编辑功能传递数据时遇到问题的解决方法

    vue项目中做编辑功能传递数据时遇到问题的解决方法

    这篇文章主要介绍了vue项目中做编辑功能传递数据时遇到问题的解决方法,vue父组件向子组件传递数据的问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • 解决vue-router 嵌套路由没反应的问题

    解决vue-router 嵌套路由没反应的问题

    这篇文章主要介绍了解决vue-router 嵌套路由没反应的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • vue+swiper实现左右滑动的测试题功能

    vue+swiper实现左右滑动的测试题功能

    这篇文章主要介绍了vue+swiper实现左右滑动的测试题功能,本文通过实例代码给大介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • vue element el-select下拉滚动加载的方法

    vue element el-select下拉滚动加载的方法

    很多朋友都遇到这样一个问题在使用vue+element的el-select下拉框加载返回数据太多时,会造成卡顿,用户体验欠佳,这篇文章主要介绍了vue element el-select下拉滚动加载方法,需要的朋友可以参考下
    2022-11-11
  • vue前端获取/切换麦克风、播放采集音频和采集音量大小完整代码

    vue前端获取/切换麦克风、播放采集音频和采集音量大小完整代码

    这篇文章主要给大家介绍了关于vue前端获取/切换麦克风、播放采集音频和采集音量大小的相关资料,文中通过图文以及代码介绍的非常详细,对大家学习或者使用vue具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-12-12
  • vue实现自定义公共组件及提取公共的方法

    vue实现自定义公共组件及提取公共的方法

    这篇文章主要介绍了vue实现自定义公共组件及提取公共的方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • vue+js点击箭头实现图片切换

    vue+js点击箭头实现图片切换

    这篇文章主要为大家详细介绍了vue+js点击箭头实现图片切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06

最新评论