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语言实现机器学习的K-近邻算法
2023-04-25 02:09:58
如何设置mysql允许外部连接访问
2024-01-15 05:22:14
15行Python代码实现网易云热门歌单实例教程
2023-07-08 13:33:27
机器学习10大经典算法详解
2021-02-21 01:39:57
Python使用arrow库优雅地处理时间数据详解
2023-02-26 07:14:12
python用sqlacodegen根据已有数据库(表)结构生成对应SQLAlchemy模型
2024-01-23 02:39:44
一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃
2023-03-02 15:46:48
保护SQL服务器的安全 用户识别问题
2008-12-24 15:26:00
使用python处理题库表格并转化为word形式的实现
2023-07-25 12:33:18
Django发送邮件和itsdangerous模块的配合使用解析
2023-09-11 04:29:00
php支持中文字符串分割的函数
2023-11-18 21:42:04
Git如何合并多次提交
2023-03-27 13:05:03
Python基础学习之函数和代码复用详解
2022-11-17 13:05:38
python 按不同维度求和,最值,均值的实例
2023-06-12 15:08:23
python图片验证码识别最新模块muggle_ocr的示例代码
2021-03-28 11:49:26
Python学习笔记之Django创建第一个数据库模型的方法
2024-01-14 20:46:39
PyTorch中view()与 reshape()的区别详析
2023-11-16 05:45:23
C#调用Python模块的方法
2021-04-13 15:29:10
python 工具类之Queue组件详解用法
2023-08-05 23:59:10
javascript框架设计之框架分类及主要功能
2024-04-18 09:33:40