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组件中节流函数的失效的原因和解决方法
Dec 02 Vue.js
详解vue中使用transition和animation的实例代码
Dec 12 Vue.js
Vue如何跨组件传递Slot的实现
Dec 14 Vue.js
vue中封装axios并实现api接口的统一管理
Dec 25 Vue.js
vuex的使用和简易实现
Jan 07 Vue.js
解决vue使用vant轮播组件swipe + flex时文字抖动问题
Jan 07 Vue.js
Vue中引入svg图标的两种方式
Jan 14 Vue.js
Vue CLI中模式与环境变量的深入详解
May 30 Vue.js
前端vue+express实现文件的上传下载示例
Feb 18 Vue.js
vue自定义右键菜单之全局实现
Apr 09 Vue.js
vue生命周期钩子函数以及触发时机
Apr 26 Vue.js
vue如何清除浏览器历史栈
May 25 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
利用yahoo汇率接口实现实时汇率转换示例 汇率转换器
2014/01/14 PHP
php+mysql+jquery实现日历签到功能
2017/02/27 PHP
php实现用户注册密码的crypt加密
2017/06/08 PHP
新闻内页-JS分页
2006/06/07 Javascript
在视频前插入广告
2006/11/20 Javascript
轻轻松松学JS调试(不下载任何工具)
2010/04/14 Javascript
在Javascript里访问SharePoint列表数据的实现方法
2011/05/22 Javascript
node.js入门教程
2014/06/01 Javascript
基于NodeJS的前后端分离的思考与实践(三)轻量级的接口配置建模框架
2014/09/26 NodeJs
js实现遮罩层弹出框的方法
2015/01/15 Javascript
js将json格式的对象拼接成复杂的url参数方法
2016/05/25 Javascript
JQuery 获取Dom元素的实例讲解
2017/07/08 jQuery
NodeJS爬虫实例之糗事百科
2017/12/14 NodeJs
小程序实现五星点评效果
2018/11/03 Javascript
JavaScript使用Math.random()生成简单的验证码
2019/01/21 Javascript
零基础之Node.js搭建API服务器的详解
2019/03/08 Javascript
Jquery的autocomplete插件用法及参数讲解
2019/03/12 jQuery
详解mpvue开发微信小程序基础知识
2019/09/23 Javascript
vue中echarts图表大小适应窗口大小且不需要刷新案例
2020/07/19 Javascript
JS禁用右键、禁用Ctrl+u、禁用Ctrl+s、禁用F12的实现代码
2020/12/01 Javascript
react的hooks的用法详解
2020/10/12 Javascript
React Native登录之指纹登录篇的示例代码
2020/11/03 Javascript
微信小程序向Java后台传输参数的方法实现
2020/12/10 Javascript
Python实现去除列表中重复元素的方法小结【4种方法】
2018/04/27 Python
pandas中去除指定字符的实例
2018/05/18 Python
matplotlib quiver箭图绘制案例
2020/04/17 Python
Python scrapy爬取小说代码案例详解
2020/07/09 Python
Pycharm2020.1安装中文语言插件的详细教程(不需要汉化)
2020/08/07 Python
俄罗斯卫浴采暖及维修用品超级市场:Dkrussia
2020/05/12 全球购物
2015大学迎新晚会主持词
2015/07/16 职场文书
你会写请假条吗?
2019/06/26 职场文书
oracle表分区的概念及操作
2021/04/24 Oracle
详解PHP服务器如何在有限的资源里最大提升并发能力
2021/05/25 PHP
MySQL 使用索引扫描进行排序
2021/06/20 MySQL
springboot临时文件存储目录配置方式
2021/07/01 Java/Android
Win10 Anaconda安装python-pcl
2022/04/29 Servers