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 相关文章推荐
vue中音频wavesurfer.js的使用方法
Feb 20 Vue.js
Vue 打包的静态文件不能直接运行的原因及解决办法
Nov 19 Vue.js
解决vue下载后台传过来的乱码流的问题
Dec 05 Vue.js
Vue——解决报错 Computed property &quot;****&quot; was assigned to but it has no setter.
Dec 19 Vue.js
vuex的使用步骤
Jan 06 Vue.js
vue-resource 拦截器interceptors使用详解
Jan 18 Vue.js
vue使用transition组件动画效果的实例代码
Jan 28 Vue.js
SSM VUE Axios详解
Oct 05 Vue.js
如何用vue实现网页截图你知道吗
Nov 17 Vue.js
在vue中import()语法不能传入变量的问题及解决
Apr 01 Vue.js
vue组件vue-esign实现电子签名
Apr 21 Vue.js
vue递归实现树形组件
Jul 15 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
3
2006/10/09 PHP
Paypal实现循环扣款(订阅)功能
2017/03/23 PHP
PHP自定义错误处理的方法分析
2018/12/19 PHP
yii框架数据库关联查询操作示例
2019/10/14 PHP
使用jQuery的将桌面应用程序引入浏览器
2010/11/19 Javascript
如何确保JavaScript的执行顺序 之jQuery.html深度分析
2011/03/03 Javascript
Jquery封装tab自动切换效果的具体实现
2013/07/13 Javascript
JavaScript 动态加载脚本和样式的方法
2015/04/13 Javascript
Javascript基础学习笔记(菜鸟必看篇)
2016/07/22 Javascript
jquery实现轮播图效果
2017/02/13 Javascript
关于vue.js过渡css类名的理解(推荐)
2017/04/10 Javascript
从零开始学习Node.js系列教程六:EventEmitter发送和接收事件的方法示例
2017/04/13 Javascript
关于Vue.nextTick()的正确使用方法浅析
2017/08/25 Javascript
关于Vue单页面骨架屏实践记录
2017/12/13 Javascript
vue插件开发之使用pdf.js实现手机端在线预览pdf文档的方法
2018/07/12 Javascript
微信小程序实现二维码签到考勤系统
2020/01/16 Javascript
微信小程序scroll-view的滚动条设置实现
2020/03/02 Javascript
Vue props中Object和Array设置默认值操作
2020/07/30 Javascript
js实现鼠标点击飘爱心效果
2020/08/19 Javascript
Python 初始化多维数组代码
2008/09/06 Python
Python常用模块用法分析
2014/09/08 Python
Python连接phoenix的方法示例
2017/09/29 Python
Python中pow()和math.pow()函数用法示例
2018/02/11 Python
基于python图像处理API的使用示例
2020/04/03 Python
python+opencv3.4.0 实现HOG+SVM行人检测的示例代码
2021/01/28 Python
pycharm最新激活码有效期至2100年(亲测可用)
2021/02/05 Python
css3中检验表单的required,focus,valid和invalid样式
2014/02/21 HTML / CSS
linux面试题参考答案(9)
2015/01/07 面试题
生物技术专业研究生自荐信
2013/09/22 职场文书
建筑结构施工专业推荐信
2014/02/21 职场文书
授权委托书样本
2014/04/03 职场文书
政府门卫岗位职责
2014/04/29 职场文书
离婚协议书怎么写(范本参考)
2014/09/30 职场文书
Python 实现Mac 屏幕截图详解
2021/10/05 Python
JPA 通过Specification如何实现复杂查询
2021/11/23 Java/Android
JS class语法糖的深入剖析
2022/07/07 Javascript