PasswordInput 密码输入框

介绍

带网格的输入框组件,可以用于输入支付密码、短信验证码等,通常与数字键盘组件配合使用

引入

  1. import Vue from 'vue';
  2. import { PasswordInput, NumberKeyboard } from 'vant';
  3. Vue.use(PasswordInput);
  4. Vue.use(NumberKeyboard);

代码演示

基础用法

  1. <!-- 密码输入框 -->
  2. <van-password-input
  3. :value="value"
  4. info="密码为 6 位数字"
  5. :focused="showKeyboard"
  6. @focus="showKeyboard = true"
  7. />
  8. <!-- 数字键盘 -->
  9. <van-number-keyboard
  10. :show="showKeyboard"
  11. @input="onInput"
  12. @delete="onDelete"
  13. @blur="showKeyboard = false"
  14. />
  1. export default {
  2. data() {
  3. return {
  4. value: '123',
  5. showKeyboard: true,
  6. };
  7. },
  8. methods: {
  9. onInput(key) {
  10. this.value = (this.value + key).slice(0, 6);
  11. },
  12. onDelete() {
  13. this.value = this.value.slice(0, this.value.length - 1);
  14. },
  15. },
  16. };

自定义长度

  1. <van-password-input
  2. :value="value"
  3. :length="4"
  4. :gutter="15"
  5. :focused="showKeyboard"
  6. @focus="showKeyboard = true"
  7. />

明文展示

  1. <van-password-input
  2. :value="value"
  3. :mask="false"
  4. :focused="showKeyboard"
  5. @focus="showKeyboard = true"
  6. />

错误提示

通过error-info属性可以设置错误提示信息,例如当输入六位时提示密码错误

  1. <!-- 密码输入框 -->
  2. <van-password-input
  3. :value="value"
  4. :error-info="errorInfo"
  5. :focused="showKeyboard"
  6. @focus="showKeyboard = true"
  7. />
  8. <!-- 数字键盘 -->
  9. <van-number-keyboard
  10. :show="showKeyboard"
  11. @input="onInput"
  12. @delete="onDelete"
  13. @blur="showKeyboard = false"
  14. />
  1. export default {
  2. data() {
  3. return {
  4. value: '123',
  5. showKeyboard: true,
  6. errorInfo: '',
  7. };
  8. },
  9. methods: {
  10. onInput(key) {
  11. this.value = (this.value + key).slice(0, 6);
  12. if (this.value.length === 6) {
  13. this.errorInfo = '密码错误';
  14. } else {
  15. this.errorInfo = '';
  16. }
  17. },
  18. onDelete() {
  19. this.value = this.value.slice(0, this.value.length - 1);
  20. },
  21. },
  22. };

API

Props

参数说明类型默认值
value密码值string‘’
info输入框下方文字提示string-
error-info输入框下方错误提示string-
length密码最大长度number | string6
gutter输入框格子之间的间距,如 20px 2em,默认单位为pxnumber | string0
mask是否隐藏密码内容booleantrue
focused是否已聚焦,聚焦时会显示光标booleanfalse

Events

事件名说明回调参数
focus输入框聚焦时触发-

PasswordInput 密码输入框 - 图1