vue+iview实现手机号分段输入框
作者:[暂停使用]任先阳 发布时间:2024-04-26 17:40:35
标签: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>
来源:https://blog.csdn.net/qq_39571197/article/details/84873286


猜你喜欢
- 本文为大家分享了mysql 8.0.19 winx64安装教程,供大家参考,具体内容如下1. 下载mysql-8.0.19-winx641.
- 本人已经在运维行业工作了将近十年,我最早接触Linux是在大二的样子,那时候只追求易懂,所以就选择了Ubuntu作为学习、使用的对象,它简单
- 前言在写程序时,我们会经常碰到程序出现异常,这时候我们就不得不处理这些异常,以保证程序的健壮性。处理异常的版本有以下几种,你通常的做法是哪种
- 1.哆啦A梦“只要把愿望系在竹竿上请求月亮女神,心愿便能达成”。我超喜欢这句话。哆啦A梦的创造要追溯
- Step1:确定操作系统Python 解释器的下载地址为:https://www.python.org/ ,点击&nbs
- VSCode 的 Remote Deployment 插件对 WSL2 直接提供了支持,能够很方便的连接本机的 WSL2 ,但是并没有提供一
- 发现一个非常强的CSS在线排版:CSS Text Wrapper只要你拖拽线条,你就可以得到你想要的文字版式CSS代码。可以让想让文本块呈现
- function cal_pace(d,h,m,s){ var distance = d; var hours = h;
- 本文要介绍的爬虫是抓取暴走漫画上的GIF趣图,方便离线观看。爬虫用的是python3.3开发的,主要用到了urllib、request和Be
- 1. 从字典创建DataFrame>>> import pandas>>> dict_a = {'
- 实现思路1、场地部署:我们需要拥有一个可以用来画节点的地方!详看我这篇文章QGraphicsScene、QGraphicsView的基础使用
- 1.创建一个项目django-admin.py startproject HelloWorld2.进入HelloWorld项目,在manag
- 一、必备技能1、logging模块的使用(1)5个日志等级/以及5个输出日志的内置函数(2)日志收集器、日志输出渠道的概念(3)如何自定义日
- 一、前言确保安装scikit-imagenumpy二、Dataset一个例子:# 导入需要的包import torchimport torc
- 子查询可以完成 SQL 查询中比较复杂的情况,本章主要介绍一些子查询的简单用法。一、简单子查询1、简单子查询子查询是 SELECT 语句内的
- 基本环境配置python 3.6pycharmrequestscsvtime相关模块pip安装即可目标网页分析网页一切的一切都在图里找到数据
- 在 Python 中,一般情况下我们可能直接用自带的 logging 模块来记录日志,包括我之前的时候也是一样。在使用时我们需要配置一些 H
- 缺省的Perl调试器就是perl解释器本身,另外还有图形界面的调试器。由于在开发程序时一般都使用telnet访问服务器,因此本文主要为大家介
- get方法代码实现# coding:utf-8import jsonfrom urlparse import parse_qsfrom ws
- 先介绍一下SQLSERVER中的存储类对象,哈哈,先介绍一下概念嘛,让新手老手都有一个认知SQLSERVER Management Stud