vue登录验证码格子组件(做组件使用)

 

<template>
  <div class="row-center captcha_input_wrapper">
    <input
      v-for="(item, index) in captchas"
      :key="index"
      v-model="item.num"
      :id="'captcha' + index"
      @input="inputFinash(index)"
      @focus="adjust(index)"
      @keydown="inputDirection(index)"
      class="captcha_input_box row-center"
      :class="[index <= activeInput ? 'active' : '']"
      type="tel"
      maxlength="1"
    />
  </div>
</template>
  
  <script>
export default {
  data() {
    return {
      // 当前输入框
      activeInput: 0,
      captchas: [
        { num: "" },
        { num: "" },
        { num: "" },
        { num: "" },
        { num: "" },
        { num: "" },
      ],
    };
  },
  //   页面加载后聚焦第一个
  mounted() {
    let dom = document.getElementById("captcha" + this.activeInput);
    dom.focus();
  },

  methods: {
    // 自动校准输入顺序
    adjust(index) {
      let dom = document.getElementById("captcha" + this.activeInput);
      if (index !== this.activeInput && dom) {
        dom.focus();
      }
    },
    // 控制前后方向
    inputDirection(index) {
      let val = this.captchas[index].num;
      // 回退键处理
      if (event.keyCode == 8 && val == "") {
        // 重新校准
        let dom = document.getElementById("captcha" + (index - 1));
        this.activeInput = index - 1;
        if (dom) dom.focus();
      }
      if (event.keyCode != 8 && val != "") {
        let dom = document.getElementById("captcha" + (index + 1));
        this.activeInput = index + 1;
        if (dom) dom.focus();
      }
    },
    // 输入框相互联动
    inputFinash(index) {
      let val = this.captchas[index].num;
      this.activeInput = val ? index + 1 : index - 1;
      let dom = document.getElementById("captcha" + this.activeInput);
      if (dom) dom.focus();
      if (index == this.captchas.length - 1) {
        let code = this.captchas.map((x) => x.num).join("");
        if (code.length == 6) {
          this.$emit("finish", code);
        }
      }
    },
  },
};
</script>
  
  <style lang='scss'>
.row-center {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
.captcha_input_wrapper {
  width: 100%;
}
.captcha_input_box {
  width: 40px;
  height: 40px;
  margin-right: 12px;
  background: rgba(255, 255, 255, 1);
  border-radius: 6px;
  border: 1px solid #dddddd;
  font-size: 16px;
  text-align: center;
  color: #1e243a;
  outline: none;
}
.active {
  border: 1px solid #3370ff !important;
}
</style>
  

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue手机验证码登录组件需要包含以下功能: 1. 输入手机号码和验证码 2. 点击发送验证码按钮,发送验证码到手机 3. 倒计时功能,倒计时结束后可以重新发送验证码 4. 验证码输入框自动聚焦 5. 手机号码和验证码的实时校验 6. 登录按钮,点击后根据手机号码和验证码进行登录 下面是一个简单的示例代码: ```vue <template> <div> <input type="text" v-model="phone" placeholder="请输入手机号码" @input="validatePhone"> <div> <input type="text" v-model="code" placeholder="请输入验证码" @input="validateCode" ref="codeInput"> <button :disabled="countdown > 0" @click="sendCode">{{ buttonText }}</button> </div> <button :disabled="!isValid" @click="login">登录</button> </div> </template> <script> export default { data() { return { phone: '', code: '', countdown: 0, isValid: false } }, computed: { buttonText() { return this.countdown > 0 ? `${this.countdown}s` : '发送验证码' } }, methods: { validatePhone() { // TODO: 手机号码校验 this.isValid = this.phone.length === 11 && /^\d+$/.test(this.phone) }, validateCode() { // TODO: 验证码校验 this.isValid = this.code.length === 6 && /^\d+$/.test(this.code) }, sendCode() { // TODO: 发送验证码到手机 this.countdown = 60 const timer = setInterval(() => { this.countdown -= 1 if (this.countdown === 0) { clearInterval(timer) } }, 1000) this.$refs.codeInput.focus() }, login() { // TODO: 手机号码和验证码登录 } } } </script> ``` 在上面的示例代码中,我们定义了一个包含手机号码和验证码输入框、发送验证码按钮和登录按钮的组件。其中,手机号码和验证码输入框通过 v-model 指令绑定 data 中的 phone 和 code 数据,发送验证码按钮通过 @click 监听点击事件,调用 sendCode 方法发送验证码,并且通过计时器实现倒计时功能,在发送验证码的时候将验证码输入框的焦点聚焦到输入框中,登录按钮通过 :disabled 控制是否可以点击,如果手机号码和验证码都符合要求,则 isValid 为 true,登录按钮才能点击。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值