Skip to content

PasswordInput 密码输入框 - Vant 4

🔐 PasswordInput 密码输入框

🎯 介绍

PasswordInput 组件是带网格的输入框组件,可以用于输入密码、短信验证码等场景,通常与数字键盘组件配合使用。让密码输入更加安全便捷!

📦 引入

通过以下方式来全局注册组件,更多注册方式请参考组件注册

js
import { createApp } from 'vue';
import { PasswordInput, NumberKeyboard } from 'vant';

const app = createApp();
app.use(PasswordInput);
app.use(NumberKeyboard);

🎯 代码演示

🔧 基础用法

搭配数字键盘组件来实现密码输入功能,就像一个安全的数字保险箱 🔒

html
<template>
  <div class="demo-password-basic">
    <van-password-input
      :value="value"
      info="密码为 6 位数字"
      :focused="showKeyboard"
      @focus="showKeyboard = true"
    />
    <van-number-keyboard
      v-model:show="showKeyboard"
      @input="onInput"
      @delete="onDelete"
      @blur="showKeyboard = false"
    />
    <p class="input-info">当前输入:{{ value }}</p>
  </div>
</template>

<script>
import { ref } from 'vue';
import { showToast } from 'vant';

export default {
  setup() {
    const value = ref('123');
    const showKeyboard = ref(true);
    
    const onInput = (key) => {
      if (value.value.length < 6) {
        value.value += key;
        if (value.value.length === 6) {
          showToast('密码输入完成 ✅');
        }
      }
    };
    
    const onDelete = () => {
      value.value = value.value.slice(0, -1);
    };
    
    return {
      value,
      showKeyboard,
      onInput,
      onDelete
    };
  },
};
</script>

<style>
.demo-password-basic {
  padding: 20px;
}
.input-info {
  margin-top: 15px;
  text-align: center;
  color: #666;
}
</style>

📏 自定义长度

通过 length 属性来设置密码长度,适应不同的安全需求 🎯

html
<template>
  <div class="demo-password-length">
    <van-password-input
      :value="value"
      :length="4"
      info="请输入 4 位密码"
      :focused="showKeyboard"
      @focus="showKeyboard = true"
    />
    <van-number-keyboard
      v-model:show="showKeyboard"
      @input="onInput"
      @delete="onDelete"
      @blur="showKeyboard = false"
    />
    <p class="length-info">4位密码模式</p>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const value = ref('');
    const showKeyboard = ref(false);
    
    const onInput = (key) => {
      if (value.value.length < 4) {
        value.value += key;
      }
    };
    
    const onDelete = () => {
      value.value = value.value.slice(0, -1);
    };
    
    return {
      value,
      showKeyboard,
      onInput,
      onDelete
    };
  },
};
</script>

<style>
.demo-password-length {
  padding: 20px;
}
.length-info {
  margin-top: 10px;
  text-align: center;
  font-weight: bold;
}
</style>

格子间距

通过 gutter 属性来设置格子之间的间距,让输入框更有层次感 📐

html
<template>
  <div class="demo-password-gutter">
    <van-password-input
      :value="value"
      :gutter="10"
      info="格子间距为 10px"
      :focused="showKeyboard"
      @focus="showKeyboard = true"
    />
    <van-number-keyboard
      v-model:show="showKeyboard"
      @input="onInput"
      @delete="onDelete"
      @blur="showKeyboard = false"
    />
    <p class="gutter-info">间距让输入更清晰 ✨</p>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const value = ref('');
    const showKeyboard = ref(false);
    
    const onInput = (key) => {
      if (value.value.length < 6) {
        value.value += key;
      }
    };
    
    const onDelete = () => {
      value.value = value.value.slice(0, -1);
    };
    
    return {
      value,
      showKeyboard,
      onInput,
      onDelete
    };
  },
};
</script>

<style>
.demo-password-gutter {
  padding: 20px;
}
.gutter-info {
  margin-top: 10px;
  text-align: center;
  color: #1989fa;
}
</style>

明文展示

mask 设置为 false 可以明文展示输入的内容,适用于短信验证码等场景 📱

html
<template>
  <div class="demo-password-mask">
    <van-password-input
      :value="value"
      :mask="false"
      info="验证码明文显示"
      :focused="showKeyboard"
      @focus="showKeyboard = true"
    />
    <van-number-keyboard
      v-model:show="showKeyboard"
      @input="onInput"
      @delete="onDelete"
      @blur="showKeyboard = false"
    />
    <p class="mask-info">验证码模式:{{ value }}</p>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const value = ref('');
    const showKeyboard = ref(false);
    
    const onInput = (key) => {
      if (value.value.length < 6) {
        value.value += key;
      }
    };
    
    const onDelete = () => {
      value.value = value.value.slice(0, -1);
    };
    
    return {
      value,
      showKeyboard,
      onInput,
      onDelete
    };
  },
};
</script>

<style>
.demo-password-mask {
  padding: 20px;
}
.mask-info {
  margin-top: 10px;
  text-align: center;
  font-style: italic;
}
</style>

提示信息

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

html
<template>
  <div class="demo-password-info">
    <van-password-input
      :value="value"
      :info="errorInfo ? '' : '请输入 6 位密码'"
      :error-info="errorInfo"
      :focused="showKeyboard"
      @focus="showKeyboard = true"
    />
    <van-number-keyboard
      v-model:show="showKeyboard"
      @input="onInput"
      @delete="onDelete"
      @blur="showKeyboard = false"
    />
    <p class="info-status">
      状态:{{ errorInfo ? '❌ 错误' : '✅ 正常' }}
    </p>
  </div>
</template>

<script>
import { ref, watch } from 'vue';
import { showToast } from 'vant';

export default {
  setup() {
    const value = ref('123');
    const errorInfo = ref('');
    const showKeyboard = ref(true);
    
    const onInput = (key) => {
      if (value.value.length < 6) {
        value.value += key;
      }
    };
    
    const onDelete = () => {
      value.value = value.value.slice(0, -1);
    };
    
    watch(value, (newVal) => {
      if (newVal.length === 6 && newVal !== '123456') {
        errorInfo.value = '密码错误,请重新输入';
        showToast('密码错误 ❌');
      } else {
        errorInfo.value = '';
        if (newVal.length === 6) {
          showToast('密码正确 ✅');
        }
      }
    });
    
    return {
      value,
      errorInfo,
      showKeyboard,
      onInput,
      onDelete
    };
  },
};
</script>

<style>
.demo-password-info {
  padding: 20px;
}
.info-status {
  margin-top: 15px;
  text-align: center;
  font-weight: bold;
}
</style>

API

Props

参数说明类型默认值
value密码值,控制输入框的内容 🔐string''
info输入框下方文字提示,给用户友好的指引 💡string-
error-info输入框下方错误提示,显示错误信息 ⚠️string-
length密码最大长度,定义输入框的格子数量 📏number | string6
gutter输入框格子之间的间距,如 20px 2em,默认单位为px 📐number | string0
mask是否隐藏密码内容,保护隐私安全 👁️booleantrue
focused是否已聚焦,聚焦时会显示光标 🎯booleanfalse

Events

事件名说明回调参数
focus输入框聚焦时触发,开始输入状态 🎯-

类型定义

组件导出以下类型定义:

ts
import type { PasswordInputProps } from 'vant';

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称默认值描述
--van-password-input-height50px输入框高度 📏
--van-password-input-margin0 var(--van-padding-md)外边距 📐
--van-password-input-font-size20px字体大小 ✏️
--van-password-input-radius6px圆角大小 🔘
--van-password-input-backgroundvar(--van-background-2)背景色 🎨
--van-password-input-info-colorvar(--van-text-color-2)提示文字颜色 💡
--van-password-input-info-font-sizevar(--van-font-size-md)提示文字大小 📝
--van-password-input-error-info-colorvar(--van-danger-color)错误提示颜色 ⚠️
--van-password-input-dot-size10px密码圆点大小 🔴
--van-password-input-dot-colorvar(--van-text-color)密码圆点颜色 ⚫
--van-password-input-text-colorvar(--van-text-color)文字颜色 ✏️
--van-password-input-cursor-colorvar(--van-text-color)光标颜色 📍
--van-password-input-cursor-width1px光标宽度 📏
--van-password-input-cursor-height40%光标高度 📐
--van-password-input-cursor-duration1s光标动画时长 ⏱️

📚 相关文档

基於Vant構建的企業級移動端解決方案