vue+iview实现手机号分段输入框
作者:[暂停使用]任先阳 时间:2024-04-26 17:40:35
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
标签:vue,iview,输入框
0
投稿
猜你喜欢
Python中的Pandas 时间函数 time 、datetime 模块和时间处理基础讲解
2022-08-01 05:18:41
python使用SQLAlchemy操作MySQL
2024-01-16 19:11:11
Excel和Access之间的数据交换
2008-11-20 16:53:00
Ubuntu手动安装mysql5.7.10
2024-01-16 11:53:56
Python2与Python3关于字符串编码处理的差别总结
2022-05-21 19:09:51
Python文件操作和数据格式详解(简单简洁)
2022-12-18 21:28:01
Python正则表达式中flags参数的实例详解
2021-09-23 10:43:41
python argparse模块通过后台传递参数实例
2021-05-08 04:13:17
微信小程序报错:this.setData is not a function的解决办法
2024-04-19 09:47:17
vue-cli的eslint相关用法
2024-05-02 17:08:46
Python中注释(多行注释和单行注释)的用法实例
2023-07-04 15:05:01
Go语言interface详解
2023-07-05 16:55:33
Python批量对word文档进行操作步骤
2022-07-24 03:37:36
ASP生成静态模版技术(带参数的标签)
2009-03-03 12:29:00
Python超简单容易上手的画图工具库(适合新手)
2021-12-06 04:05:23
asp如何从数据库中删除废旧的电子信箱地址?
2009-11-15 20:04:00
SQL Server数据库对上亿表的操作
2008-11-16 18:13:00
读取input:file的路径并显示本地图片的方法
2024-04-17 10:38:30
Web性能优化系列 10个提升JavaScript性能的技巧
2024-05-28 15:40:37
python去除字符串中的空格、特殊字符和指定字符的三种方法
2022-06-12 20:21:31