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. />

明文展示

<van-password-input
  :value="value"
  :mask="false"
  :focused="showKeyboard"
  @focus="showKeyboard = true"
/>

错误提示

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

<!-- 密码输入框 -->
<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);
    }
  }
}

API

Props

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

Events

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

PasswordInput 密码输入框 - 图1