vue+iview实现手机号分段输入框


Posted in Vue.js onMarch 25, 2022

vue + iview 实现一个手机分段的提示框,知识点还没总结,供大家参考,具体内容如下

vue+iview实现手机号分段输入框

<template>
  <div :class="{'ivu-form-item-error':!valid && dirty && validated}">
    <div class="ivu-phone-input ivu-select  ivu-select-multiple ivu-select-default" @keydown.delete.prevent @click.stop>
      <input type="text" class="ivu-select-selection number-block"
             v-for="(item,index) in phoneLength" :key="index"
             :ref="numberRefName+index"
             @focus="handlerFocus"
             @input="handlerInput($event,index)"
             @keydown.delete.prevent="deleteNumber($event,index)"
             @keydown.left.prevent="changeInput(index - 1)"
             @keydown.right="changeInput(index + 1)"
      />
      <Icon type="ios-close-circle" class="clean-btn" @click="cleanValue"/>
    </div>
  </div>
</template>
 
<script>
  export default {
    data() {
      return {
        required: this.$attrs.hasOwnProperty('required'),
        phoneLength: 11,
        phoneReg: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
        numberRefName: 'numberBlock',
        validTimer: null,
        dirty: false,
        valid: false,
        validated: false,
      };
    },
    methods: {
 
      handlerFocus() {
        if (!this.dirty) {
          this.dirty = this.required ? true : false;
        }
      },
 
      handlerInput(e, index) {
        if (!e.target.value) {
          return;
        }
        this.dirty = true;
        let value = e.target.value.replace(/\D+/g, '');
        value = value ? value[0] : '';
        //合法值,切换下一个输入框
        if (value.length) {
          this.changeInput(index + 1);
        }
        //#end
        e.target.value = value;
        this.debounceValidate();
      },
      changeInput(index) {
        if (index < 0 || index === this.phoneLength) return;
        const target = this.$refs[this.numberRefName + index][0];
        target.focus();
        if (target.value && target.setSelectionRange) {
          target.setSelectionRange(1, 1);//maxlength="1" 时无效,所以去掉了...
        }
      },
      deleteNumber(e, index) {
        if (e.target.value) {
          e.target.value = ''
        } else {
          this.changeInput(index - 1);
        }
      },
      resetStatus() {
        this.validated = false;
        this.dirty = false;
      },
      cleanValue() {
        this.resetStatus();
        const numberBlocks = this.$refs;
        for (let i in numberBlocks) {
          numberBlocks[i][0].value = '';
        }
        if (this.required) {
          const FormItem = this.getFormItem();
          if (FormItem) {
            FormItem.resetField();
            FormItem.$emit('on-form-change', null);
          }
        }
        // this.changeInput(0);
      },
      debounceValidate() {
        this.validTimer = setTimeout(() => {
          this.validate();
        }, 300);
      },
      validate(isLeave) {
        const numberBlocks = this.$refs;
        let result = '';
        for (let i in numberBlocks) {
          result += numberBlocks[i][0].value;
        }
        if (result.length === this.phoneLength || isLeave) {
          this.validated = true;
          this.dispath({
            value: result,
            valid: this.valid = this.phoneReg.test(result),
          });
        }
      },
      dispath(info) {
        this.$emit('input', info.valid ? info.value : '');
        if (this.required) {
          const FormItem = this.getFormItem();
          if (FormItem) {
            this.updateFormItem(FormItem, info.valid ? info.value : '');
          }
        }
      },
      getFormItem() {
        let MAX_LEVEL = 3;
        let parent = this.$parent;
        let name = parent.$options.name;
        while (MAX_LEVEL && name !== 'FormItem') {
          MAX_LEVEL--;
          if (!parent) return null;
          parent = parent.$parent;
        }
        return parent || null;
      },
      updateFormItem(FormItem, data) {
        FormItem.$emit('on-form-change', data);
      },
      pageEvent() {
        if (this.dirty) {
          this.validate(true);
        }
      },
    },
    created() {
      window.addEventListener('click', this.pageEvent);
    },
    beforeDestroy() {
      window.removeEventListener('click', this.pageEvent);
    },
  };
</script>
 
<style scoped lang="less">
  .ivu-phone-input {
    .clean-btn {
      transition: opacity .5s;
      opacity: 0;
      cursor: pointer;
    }
    &:hover {
      .clean-btn {
        opacity: 1;
      }
    }
  }
 
  .number-block {
    display: inline-block;
    padding: 0;
    height: 30px;
    width: 28px;
    text-align: center;
    margin-right: 2px;
    &:nth-child(3) {
      margin-right: 10px;
    }
    &:nth-child(7) {
      margin-right: 10px;
    }
  }
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
详解Vue2的diff算法
Jan 06 Vue.js
vue watch监控对象的简单方法示例
Jan 07 Vue.js
vuex的使用和简易实现
Jan 07 Vue.js
Vue中引入svg图标的两种方式
Jan 14 Vue.js
vue使用过滤器格式化日期
Jan 20 Vue.js
如何在vue 中使用柱状图 并自修改配置
Jan 21 Vue.js
Vue2.x-使用防抖以及节流的示例
Mar 02 Vue.js
vue使用v-model进行跨组件绑定的基本实现方法
Apr 28 Vue.js
vue-cli4.5.x快速搭建项目
May 30 Vue.js
vue实现列表拖拽排序的示例代码
Apr 08 Vue.js
vue elementUI表格控制对应列
Apr 13 Vue.js
vue封装数字翻牌器
Apr 20 Vue.js
Vue3中toRef与toRefs的区别
Mar 24 #Vue.js
一起来看看Vue的核心原理剖析
Mar 24 #Vue.js
深入讲解Vue中父子组件通信与事件触发
Mar 22 #Vue.js
关于Vue中的options选项
Mar 22 #Vue.js
vue+echarts实现多条折线图
vue使用echarts实现折线图
浅谈Vue的computed计算属性
You might like
欧美媒体选出10年前最流行的17部动画
2017/01/18 日漫
php 文件上传代码(限制jpg文件)
2010/01/05 PHP
推荐5款跨平台的PHP编辑器
2014/12/25 PHP
php实现将任意进制数转换成10进制的方法
2015/04/17 PHP
Javascript实现返回上一页面并刷新的小例子
2013/12/11 Javascript
jquery的ajax简单结构示例代码
2014/02/17 Javascript
jQuery调用ajax请求的常见方法汇总
2015/03/24 Javascript
详解javascript数组去重问题
2015/11/06 Javascript
深入理解JavaScript中的对象复制(Object Clone)
2016/05/18 Javascript
轻松掌握JavaScript单例模式
2016/08/25 Javascript
浅述Javascript的外部对象
2016/12/07 Javascript
Vue 获取数组键名的方法
2018/06/21 Javascript
傻瓜式解读koa中间件处理模块koa-compose的使用
2018/10/30 Javascript
TypeScript中使用getElementXXX()的示例代码
2019/09/12 Javascript
nodejs使用socket5进行代理请求的实现
2020/02/21 NodeJs
python中去空格函数的用法
2014/08/21 Python
python的Template使用指南
2014/09/11 Python
Python实现堆排序的方法详解
2016/05/03 Python
Python实现的中国剩余定理算法示例
2017/08/05 Python
selenium+python实现1688网站验证码图片的截取功能
2018/08/14 Python
python发送多人邮件没有展示收件人问题的解决方法
2019/06/21 Python
图文详解Django使用Pycharm连接MySQL数据库
2019/08/09 Python
python连接打印机实现打印文档、图片、pdf文件等功能
2020/02/07 Python
如何利用python进行时间序列分析
2020/08/04 Python
python 浮点数四舍五入需要注意的地方
2020/08/18 Python
完美解决torch.cuda.is_available()一直返回False的玄学方法
2021/02/06 Python
英国优质家居用品网上品牌:URBANARA
2018/06/01 全球购物
波兰在线体育用品商店:Hop-Sport.pl
2019/07/23 全球购物
全球工业:Global Industrial
2020/02/01 全球购物
do you have any Best Practice for testing
2016/06/04 面试题
人事部专员岗位职责
2014/03/04 职场文书
2014年学生会主席工作总结
2014/11/07 职场文书
2014年护理部工作总结
2014/11/14 职场文书
鸦片战争观后感
2015/06/09 职场文书
2019大学生暑期实习心得总结
2019/08/21 职场文书
Linux7.6二进制安装Mysql8.0.27详细操作步骤
2021/11/27 MySQL