May 07, 2021 Vant
A gridded input box component that can be used to enter payment passwords, SMS verification codes, and so on, usually in conjunction with the numeric keypad component
import Vue from 'vue';
import { PasswordInput, NumberKeyboard } from 'vant';
Vue.use(PasswordInput);
Vue.use(NumberKeyboard);
<!-- 密码输入框 -->
<van-password-input
:value="value"
info="密码为 6 位数字"
:focused="showKeyboard"
@focus="showKeyboard = true"
/>
<!-- 数字键盘 -->
<van-number-keyboard
:show="showKeyboard"
@input="onInput"
@delete="onDelete"
@blur="showKeyboard = false"
/>
export default {
data() {
return {
value: '123',
showKeyboard: true
};
},
methods: {
onInput(key) {
this.value = (this.value + key).slice(0, 6);
},
onDelete() {
this.value = this.value.slice(0, this.value.length - 1);
}
}
}
<van-password-input
:value="value"
:length="4"
:gutter="15"
:focused="showKeyboard"
@focus="showKeyboard = true"
/>
<van-password-input
:value="value"
:mask="false"
:focused="showKeyboard"
@focus="showKeyboard = true"
/>
The error-info property allows you to set an error message, such as a password error when entering six bits
<!-- 密码输入框 -->
<van-password-input
:value="value"
:error-info="errorInfo"
:focused="showKeyboard"
@focus="showKeyboard = true"
/>
<!-- 数字键盘 -->
<van-number-keyboard
:show="showKeyboard"
@input="onInput"
@delete="onDelete"
@blur="showKeyboard = false"
/>
export default {
data() {
return {
value: '123',
showKeyboard: true,
errorInfo: ''
};
},
methods: {
onInput(key) {
this.value = (this.value + key).slice(0, 6);
if (this.value.length === 6) {
this.errorInfo = '密码错误';
} else {
this.errorInfo = '';
}
},
onDelete() {
this.value = this.value.slice(0, this.value.length - 1);
}
}
}
Parameters | Description | Type | The default |
---|---|---|---|
value | The password value | string |
''
|
info | Enter the text hint below the box | string | - |
error-info | The error prompt below the input box | string | - |
length | The maximum length of the password | number | string |
6
|
gutter |
Enter the spacing between the boxes,
20px
2em
in
px
|
number | string |
0
|
mask | Whether to hide the contents of the password | boolean |
true
|
focused
v2.1.8
|
If it is focused, the cursor is displayed when it is focused | boolean |
false
|
The name of the event | Description | Callback parameters |
---|---|---|
focus | Triggered when the input box is focused | - |