vue输入框的验证码密码校验码 输入组件

参考:
1.uni-app项目自定义验证码输入、密码输入使用
2.vue实现一个6个输入框的验证码输入组件

找了一些,第一个感觉很漂亮,用的时候有一点瑕疵,遂自己重写了下
在这里插入图片描述

1.使用


<valid-code ref="code" :maxlength="4" :isPwd="false" @finish="getCode" ></valid-code>
...
...
import validCode from '../components/validCode';
...
export default {
...
components:{
    'valid-code':validCode
  }
...
methods:{
	getCode(val){
      this.$set(this.queryParams,"code",val);
    },
    clearCode(){
      this.$refs.code.clear();
    }
}

2.validcode.vue

<template>
  <div class="code-area">
    <div class="flex-box">
      <input
        v-for="n in maxlength"
        @type="isPwd ? 'password' : 'text'"
        :key="n"
        class="input-item"
        @keydown="keydownHandle"
        @keyup="keyupHandle"
        @paste="pasteHandle"
        autocomplete="off"
      />
    </div>
  </div>
</template>

<script>
export default {
  props: {
    //最大长度 值为4或者6
    maxlength: {
      type: Number,
      default: 4,
    },
    //是否是密码
    isPwd: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      input_lock: false,
      doms: [],
    };
  },
  mounted() {
    let doms = document.getElementsByClassName("input-item");
    this.doms = doms;
    //防止中文输入法多输入
    [].forEach.call(doms, (inputDom) => {
      inputDom.addEventListener("compositionstart", () => {
        console.log("compositionstart");
        this.input_lock = true;
      });
      inputDom.addEventListener("compositionend", (event) => {
        this.input_lock = false;
        console.log("compositionend");
        this.keyupHandle(event);
      });
    });
    doms[0].focus();
  },
  methods: {
    //退格处理
    keydownHandle(event) {
      if ( this.input_lock || event.key !== "Backspace" || event.target.value != "") {
        return false;
      }

      let preDom = event.target.previousElementSibling;
      if (preDom) {
        preDom.value = "";
        preDom.focus();
      }
    },
    //键入
    keyupHandle(event) {
      if (this.input_lock) return false;
      let {
        target,
        target: { value },
      } = event;
      value = this.formatValue(value);
      if (/[^\w\.\/]/.test(value) || value == "") {
        target.value = "";
        return false;
      }
      target.value = value.charAt(value.length - 1);
      let nextDom = target.nextElementSibling;
      if (nextDom) {
        nextDom.value = "";
        nextDom.focus();
      } else {
        target.blur();
        this.getVal();
      }
    },
    //粘贴
    pasteHandle(event) {
      // 当进行粘贴时
      let paste = this.formatValue((event.clipboardData || window.clipboardData).getData("text"));
      if (paste.length == this.maxlength) {
        for (let i = 0; i < this.maxlength; i++) {
          this.doms[i].value = paste.charAt(i);
        }
        event.target.blur();
        this.getVal();
      }else{
        this.clear();
      }
      event.preventDefault();
    },
    //取值
    getVal() {
      //输入完成
      let value = [].reduce.call(this.doms, (pre, el) => pre + el.value, "");
      this.$emit("finish", value);
    },
    //清除验证码或者密码
    clear() {
      [].forEach.call(this.doms, (el) => (el.value = ""));
      this.doms[0].focus();
    },
    //格式化
    formatValue(value) {
      return value
        .replace(/[^\w\.\/]/gi, "")
        .replaceAll(".", "")
        .trim();
    },
  },
};
</script>

<style lang="scss">
input,
button,
select,
textarea {
  outline: none;
}

.btn:focus,
.btn:active:focus {
  outline: none;
  border-color: transparent;
  box-shadow: none;
}

input {
  color: #333;
  caret-color: #399ffd;
}

@supports (-webkit-mask: none) and (not (cater-color: red)) {
  input {
    color: #399ffd;
  }

  input::first-line {
    color: #333;
  }
}

.code-area {
  text-align: center;

  .flex-box {
    display: flex;
    flex-wrap: wrap;
    position: relative;
    justify-content: center;
  }

  .input-item {
    position: relative;
    width: 50px;
    height: 50px;
    margin-right: 9px;
    font-size: 15px;
    font-weight: bold;
    line-height: 50px;
    box-sizing: border-box;
    border: 1px solid #cccccc;
    text-align: center;
  }

  .input-item:last-child {
    margin-right: 0;
  }

  input:focus {
    border-color: #1890ff;
  }
}
</style>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值