iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > 其他 >vue3怎么实现​6位支付密码输入框
  • 787
分享到

vue3怎么实现​6位支付密码输入框

Vue3 2023-05-18 09:05:47 787人浏览 八月长安
摘要

具体的需求: 在客户信息表格的操作栏中,点击修改支付密码按钮,会跳转到6位支付密码输入框组件页面。同时,要求输入框密文显示、不可编辑、不可回退、即时显示;到达6位数,自动进入确认支付密码;确认支付密码到达6位数,自动检验两次输入密码的一致性

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

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

vue3怎么实现​6位支付密码输入框

一、代码总览

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

<template>
  <div >
    <!-- 密码输入框 -->
    <div class="input-box" >
      <!-- 输入密码 -->
      <div >{{ "输入密码" }}</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 >{{ "确认密码" }}</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 >
      <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
      >
      <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位数字?

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

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

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

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

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

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

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

以上就是vue3怎么实现​6位支付密码输入框的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: vue3怎么实现​6位支付密码输入框

本文链接: https://www.lsjlt.com/news/211932.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 小程序速报[No.90] | 小程序最新资讯,错过还要等2周
    ⭐本期内容速览:1、平台支付手续费调整通知2、关于canvas v2 保存图片的实现3、小程序变现锦囊:交易解决方案上线,新能力助力好生意4、交易类目限制订单金额及账期调整通知5、助力留资转化率,留资组件内测开启6、官方小程序上线,提升经营...
    99+
    2024-05-15
    头条 抖音 小程序 更新
  • 代码级质量技术之基本框架介绍
    一、背景代码级质量技术:顾名思义为了服务质量更好,涉及到代码层面的相关技术,特别要指出的是,代码级质量技术不单纯指代码召回技术,如静态代码扫描、单元测试等。研究代码级质量技术主要有以下几个方面的原因:1、随着精准测试等概念的兴起,对代码覆盖...
    99+
    2024-05-15
    代码级质量 框架
  • jquery清除同级div
    随着Web应用不断复杂,JavaScript框架变得越来越重要。其中最受欢迎的框架之一是jQuery,jQuery是一个流行的JavaScript库,它可以使处理文档元素、处理事件和创建特效更加容易。在这篇文章中,我们将学习如何使用jQue...
    99+
    2024-05-15
  • jquery 查询筛选
    JQuery是一款非常优秀的JavaScript库,提供了许多便捷的方法使得前端开发变得更加快捷高效。在JQuery中,查询筛选是其最常用的功能之一,本文将详细介绍JQuery查询筛选的相关部分。一、概述JQuery的查询筛选机制是其最重要...
    99+
    2024-05-15
  • jquery 图片无法显示
    在网页开发过程中,经常会用到图片来丰富页面内容,但有时会出现图片无法显示的情况,这个问题通常与文件路径或文件名有关。本文将介绍使用 jQuery 快速解决图片无法显示的问题。一、确认图片文件路径最常见的原因是图片文件路径错误或者图片文件不存...
    99+
    2024-05-15
  • jquery移除只读
    在使用HTML表单时,经常需要将一些输入框设置为只读状态,以防止用户对这些信息进行修改。但有时候需要在特定情况下,将这些输入框的只读属性移除,以便用户可以进行修改或编辑。这时候就需要使用jQuery来移除只读属性。jQuery是一种Java...
    99+
    2024-05-15
  • vue选择地址怎么做
    Vue是一种流行的JavaScript框架,用于构建交互式Web应用程序。在许多Web应用程序中,选择地址是常见的功能需求。本文将介绍如何使用Vue实现选择地址,包括如何使用第三方库和如何自定义Vue组件。一、使用第三方库一个常见的选择地址...
    99+
    2024-05-15
  • VUe双中括号属性如何使用
    Vue是一种流行的JavaScript框架,它使用双花括号(“{{”和“}}”)语法来实现属性与视图之间的数据绑定。VUe框架中的模板显示了被框架监控的变量的特定属性,当这些变量的值改变时,这些属性也跟随改变,从而在视图中自动更新相应的值。...
    99+
    2024-05-15
  • vue refs 方法参数
    随着前端技术的不断发展,Vue框架毫无疑问成为了前端框架中最受欢迎的一种。Vue组件的复杂性也越来越高,因此,Vue框架提供了许多API来使得开发更加灵活和高效。其中之一就是refs方法。refs 方法用于在Vue中访问组件的实例或元素。这...
    99+
    2024-05-15
  • vue.js函数求和
    Vue.js是一个流行的JavaScript框架,它使得前端开发变得更容易和更快速。在Vue.js中,我们可以通过函数来实现对数据的操作,并实现一些加减乘除等简单的数学计算。本文将介绍如何使用Vue.js实现一个函数求和功能。首先,我们需要...
    99+
    2024-05-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作