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-cli中使用css-loader实现css module
Jan 07 Vue.js
Vue实现图书管理案例
Jan 20 Vue.js
浅谈Vue开发人员的7个最好的VSCode扩展
Jan 20 Vue.js
vue keep-alive的简单总结
Jan 25 Vue.js
Vue使用Ref跨层级获取组件的步骤
Jan 25 Vue.js
Vue ​v-model相关知识总结
Jan 28 Vue.js
如何管理Vue中的缓存页面
Feb 06 Vue.js
vue前端工程的搭建
Mar 31 Vue.js
vue组件的路由高亮问题解决方法
May 11 Vue.js
vue实现移动端div拖动效果
Mar 03 Vue.js
vue使用wavesurfer.js解决音频可视化播放问题
Apr 04 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
一个比较不错的PHP日历类分享
2014/11/18 PHP
javascript String 的扩展方法集合
2008/06/01 Javascript
JavaScript 继承详解(三)
2009/07/13 Javascript
用JQuery 实现AJAX加载XML并解析的脚本
2009/07/25 Javascript
关于javascript中的parseInt使用技巧
2009/09/03 Javascript
ExtJs事件机制基本代码模型和流程解析
2010/10/24 Javascript
js和css写一个可以自动隐藏的悬浮框
2014/03/05 Javascript
使用时间戳解决ie缓存的问题
2014/08/20 Javascript
第一次接触神奇的Bootstrap
2016/10/14 Javascript
Vue的Flux框架之Vuex状态管理器
2017/07/30 Javascript
基于jQuery解决ios10以上版本缩放问题
2017/11/03 jQuery
Vue 中使用vue2-highcharts实现top功能的示例
2018/03/05 Javascript
详解vue的diff算法原理
2018/05/20 Javascript
微信小程序中换行空格(多个空格)写法详解
2018/07/10 Javascript
vue插件draggable实现拖拽移动图片顺序
2018/12/01 Javascript
使用vue实现各类弹出框组件
2019/07/03 Javascript
使用 Jest 和 Supertest 进行接口端点测试实例详解
2020/04/25 Javascript
Vue实现简单购物车功能
2020/12/13 Vue.js
[30:51]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#1Liquid VS MVP.Phx第一局
2016/03/04 DOTA
Python使用Flask框架同时上传多个文件的方法
2015/03/21 Python
windows下搭建python scrapy爬虫框架步骤
2018/12/23 Python
Python3实现的判断回文链表算法示例
2019/03/08 Python
python数据持久存储 pickle模块的基本使用方法解析
2019/08/30 Python
Python PyInstaller库基本使用方法分析
2019/12/12 Python
Django 限制访问频率的思路详解
2019/12/24 Python
零基础小白多久能学会python
2020/06/22 Python
vscode配置anaconda3的方法步骤
2020/08/08 Python
Python爬虫UA伪装爬取的实例讲解
2021/02/19 Python
.net面试题
2016/09/17 面试题
如何查找网页漏洞
2016/06/22 面试题
杠杆的科学教学反思
2014/01/10 职场文书
生日答谢词
2015/01/05 职场文书
离职证明格式样本
2015/06/12 职场文书
边城读书笔记
2015/06/29 职场文书
大学入学感言
2015/08/01 职场文书
一文读懂go中semaphore(信号量)源码
2021/04/03 Golang