Files
heroui-vue/apps/docs/demos/input-otp-with-validation.vue

71 lines
1.8 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { ref, useId } from "vue";
import {
Button,
Description,
FieldError,
Form,
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
Label,
} from "@heroui-vue/components";
const value = ref("");
const isInvalid = ref(false);
const errorId = useId();
function onSubmit(event: Event) {
event.preventDefault();
const form = event.target as HTMLFormElement;
const data = new FormData(form);
const code = data.get("code");
if (code !== "123456") {
isInvalid.value = true;
return;
}
isInvalid.value = false;
value.value = "";
alert("验证码校验成功!");
}
function handleChange() {
isInvalid.value = false;
}
</script>
<template>
<div class="flex w-[280px] flex-col gap-2">
<Form class="flex flex-col gap-2" @submit="onSubmit">
<InputOTP
v-model="value"
:is-invalid="isInvalid"
:max-length="6"
:validation-errors="isInvalid ? ['验证码无效,请重试。'] : []"
name="code"
style="flex-direction: column; align-items: stretch; gap: 0.5rem"
@update:model-value="handleChange"
>
<Label>验证账户</Label>
<Description>提示:验证码为 123456</Description>
<div class="flex items-center gap-2">
<InputOTPGroup>
<InputOTPSlot :index="0" />
<InputOTPSlot :index="1" />
<InputOTPSlot :index="2" />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot :index="3" />
<InputOTPSlot :index="4" />
<InputOTPSlot :index="5" />
</InputOTPGroup>
</div>
<FieldError :id="errorId" />
</InputOTP>
<Button :is-disabled="value.length !== 6" type="submit">提交</Button>
</Form>
</div>
</template>