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 获取到数据但却渲染不到页面上的解决方法
Nov 19 Vue.js
Vue如何跨组件传递Slot的实现
Dec 14 Vue.js
vue常用高阶函数及综合实例
Feb 25 Vue.js
Vue基本指令实例图文讲解
Feb 25 Vue.js
vue前端和Django后端如何查询一定时间段内的数据
Feb 28 Vue.js
vue中axios封装使用的完整教程
Mar 03 Vue.js
Vue项目中如何封装axios(统一管理http请求)
May 02 Vue.js
vue实现可拖拽的dialog弹框
May 13 Vue.js
Vue过滤器(filter)实现及应用场景详解
Jun 15 Vue.js
Vue实现跑马灯样式文字横向滚动
Nov 23 Vue.js
一篇文章告诉你如何实现Vue前端分页和后端分页
Feb 18 Vue.js
vue如何在data中引入图片的正确路径
Jun 05 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下用cookie统计用户访问网页次数的代码
2010/05/09 PHP
PHP 数据结构 算法描述 冒泡排序 bubble sort
2011/07/10 PHP
PHP中创建空文件的代码[file_put_contents vs touch]
2012/01/20 PHP
php使用smtp发送支持附件的邮件示例
2014/04/13 PHP
PHP code 验证码生成类定义和简单使用示例
2020/05/27 PHP
jquery解析JSON数据示例代码
2014/03/17 Javascript
js实现的二分查找算法实例
2016/01/21 Javascript
jquery获取文档高度和窗口高度汇总
2016/01/25 Javascript
VueJs路由跳转——vue-router的使用详解
2017/01/10 Javascript
微信小程序教程系列之设置标题栏和导航栏(7)
2020/06/29 Javascript
微信小程序图片宽100%显示并且不变形
2017/06/21 Javascript
浅谈JS中的常用选择器及属性、方法的调用
2017/07/28 Javascript
vue-router相关基础知识及工作原理
2018/03/16 Javascript
Jquery如何使用animation动画效果改变背景色的代码
2020/07/20 jQuery
解决ant Design中Select设置initialValue时的大坑
2020/10/29 Javascript
树莓派中python获取GY-85九轴模块信息示例
2013/12/05 Python
Python 正则表达式(转义问题)
2014/12/15 Python
Python实现一个简单的MySQL类
2015/01/07 Python
在Django框架中编写Contact表单的教程
2015/07/17 Python
python 爬取微信文章
2016/01/30 Python
Python cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例
2018/03/15 Python
python批量实现Word文件转换为PDF文件
2018/03/15 Python
python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法
2020/02/14 Python
Python爬虫实现vip电影下载的示例代码
2020/04/20 Python
详解通过HTML5 Canvas实现图片的平移及旋转变化的方法
2016/03/22 HTML / CSS
美国独家设计师眼镜在线光学商店:Glasses Gallery
2017/12/28 全球购物
Vinatis德国:法国领先的葡萄酒邮购公司
2020/09/07 全球购物
类成员函数的重载、覆盖和隐藏区别
2016/01/27 面试题
什么是View State?
2013/01/27 面试题
母亲节感恩活动记录
2014/03/16 职场文书
驾驶员培训方案
2014/05/01 职场文书
科技工作者先进事迹
2014/08/16 职场文书
2015年学校党建工作总结
2015/05/19 职场文书
修改MySQL的默认密码的四种小方法
2021/05/26 MySQL
Redis集群节点通信过程/原理流程分析
2022/03/18 Redis
Win11快速关闭所有广告推荐
2022/04/19 数码科技