博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue前端验证输入_vue实现6位验证码输入框的实例代码
阅读量:4579 次
发布时间:2019-06-08

本文共 3760 字,大约阅读时间需要 12 分钟。

要实现的功能:

完全和单输入框一样的操作,甚至可以插入覆盖:

1,限制输入数字

2,正常输入

3,backspace删除

4,paste任意位置粘贴输入

5,光标选中一个数字,滚轮可以微调数字大小,限制0-9

6,123|456 自动覆盖光标后输入的字符,此时光标在3后,继续输入111,会得到123111,而不用手动删除456

7,封装成vue单文件组件,方便任意调用。

模板代码

@input="inputEvent">

ref="firstinput"/>

实现了键盘的keydown/keyup/paste/input和鼠标滚轮mousewheel事件

使用了6个输入框的方案来实现。

样式部分:使用了scss模式

.input-box {

.input-content {

width: 512px;

height: 60px;

display: flex;

align-items: center;

justify-content: space-between;

input {

color: inherit;

font-family: inherit;

border: 0;

outline: 0;

border-bottom: 1px solid #919191;

height: 60px;

width: 60px;

font-size: 44px;

text-align: center;

}

}

input::-webkit-outer-spin-button,

input::-webkit-inner-spin-button {

appearance: none;

margin: 0;

}

}

具体实现逻辑:主要实现以上几个键盘事件操作。

export default {

data() {

return {

// 存放粘贴进来的数字

pasteResult: [],

};

},

props: ['code'],

computed: {

input() {

// code 是父组件传进来的默认值,必须是6位长度的数组,这里就不再做容错判断处理

// 最后空数组是默认值

return this.code || this.pasteResult.length === 6 ? this.pasteResult : ['', '', '', '', '', '']

}

},

methods: {

// 解决一个输入框输入多个字符

inputEvent(e) {

var index = e.target.dataset.index * 1;

var el = e.target;

this.$set(this.input, index, el.value.slice(0, 1))

},

keydown(e) {

var index = e.target.dataset.index * 1;

var el = e.target;

if (e.key === 'Backspace') {

if (this.input[index].length > 0) {

this.$set(this.input, index, '')

} else {

if (el.previousElementSibling) {

el.previousElementSibling.focus()

this.$set(this.input, index - 1, '')

}

}

} else if (e.key === 'Delete') {

if (this.input[index].length > 0) {

this.$set(this.input, index, '')

} else {

if (el.nextElementSibling) {

this.$set(this.input, index = 1, '')

}

}

if (el.nextElementSibling) {

el.nextElementSibling.focus()

}

} else if (e.key === 'Home') {

el.parentElement.children[0] && el.parentElement.children[0].focus()

} else if (e.key === 'End') {

el.parentElement.children[this.input.length - 1] && el.parentElement.children[this.input.length - 1].focus()

} else if (e.key === 'ArrowLeft') {

if (el.previousElementSibling) {

el.previousElementSibling.focus()

}

} else if (e.key === 'ArrowRight') {

if (el.nextElementSibling) {

el.nextElementSibling.focus()

}

} else if (e.key === 'ArrowUp') {

if (this.input[index] * 1 < 9) {

this.$set(this.input, index, (this.input[index] * 1 + 1).toString());

}

} else if (e.key === 'ArrowDown') {

if (this.input[index] * 1 > 0) {

this.$set(this.input, index, (this.input[index] * 1 - 1).toString());

}

}

},

keyup(e) {

var index = e.target.dataset.index * 1;

var el = e.target;

if (/Digit|Numpad/i.test(e.code)) {

this.$set(this.input, index, e.code.replace(/Digit|Numpad/i, ''));

el.nextElementSibling && el.nextElementSibling.focus();

if (index === 5) {

if (this.input.join('').length === 6) {

document.activeElement.blur();

this.$emit('complete', this.input);

}

}

} else {

if (this.input[index] === '') {

this.$set(this.input, index, '');

}

}

},

mousewheel(e) {

var index = e.target.dataset.index;

if (e.wheelDelta > 0) {

if (this.input[index] * 1 < 9) {

this.$set(this.input, index, (this.input[index] * 1 + 1).toString());

}

} else if (e.wheelDelta < 0) {

if (this.input[index] * 1 > 0) {

this.$set(this.input, index, (this.input[index] * 1 - 1).toString());

}

} else if (e.key === 'Enter') {

if (this.input.join('').length === 6) {

document.activeElement.blur();

this.$emit('complete', this.input);

}

}

},

paste(e) {

// 当进行粘贴时

e.clipboardData.items[0].getAsString(str => {

if (str.toString().length === 6) {

this.pasteResult = str.split('');

document.activeElement.blur();

this.$emit('complete', this.input);

}

})

}

},

mounted() {

// 等待dom渲染完成,在执行focus,否则无法获取到焦点

this.$nextTick(() => {

this.$refs.firstinput.focus()

})

},

}

如果你发现了bug,或者有优化空间,欢迎你的指正和建议。我会随时更新到原代码当中,分享给大家。本文转载自:https://segmentfault.com/a/1190000023041401

更多web前端开发知识,请查阅 HTML中文网 !!

你可能感兴趣的文章
Android ROM 制作教程
查看>>
Android模拟器使用SD卡
查看>>
新手Oracle安装及使用入门
查看>>
帝国cms灵动标签下常用标签
查看>>
STL学习笔记(关联式容器)
查看>>
Android生成xml
查看>>
python入到到实战--第十章----文件
查看>>
FMDataBase 打开sqlite的外键约束功能
查看>>
Nmap 7.70新增功能——扫描主机所有IP
查看>>
二分图
查看>>
UVA10559&POJ1390 Blocks 区间DP
查看>>
《Linux内核》读书笔记 第十八章
查看>>
【AS3代码】擦窗户效果(也就是流行的妄撮游戏)
查看>>
[bzoj 3289] Mato的文件管理
查看>>
Flutter学习笔记(五)
查看>>
Linux zip命令详解
查看>>
vSphere的exsi root密码忘记了
查看>>
svn的安装过程
查看>>
pure的bug记录2
查看>>
NSCopying简析
查看>>