Page·Free
Sign-up form (Vue)
A sign-up form for Vue 3 as a single-file component in TypeScript, with name, email and password. When you leave a field, it says what is wrong, and screen readers announce the same message. It uses no libraries, and the colours come from CSS variables.
Free to install.
If PocketCode is installed on your Android device, this opens the page in the app.
Preview
SignupForm.vue · 211 lines
<!--
Sign-up form for Vue 3 (script setup + TypeScript), with no libraries.
Each field says what is wrong under it after you leave it or press "Create account",
and screen readers hear the same message. The parent gets the values in `submit`:
<SignupForm :pending="saving" @submit="createAccount" />
Colours come from CSS variables with fallbacks: set --form-text, --form-muted,
--form-border, --form-error, --form-primary-bg and --form-primary-text to match your app.
-->
<script setup lang="ts">
import { computed, reactive, ref, useId } from 'vue';
export interface SignupValues {
name: string;
email: string;
password: string;
}
const props = withDefaults(defineProps<{ pending?: boolean; minPassword?: number }>(), {
pending: false,
minPassword: 8,
});
const emit = defineEmits<{ submit: [values: SignupValues] }>();
const id = useId();
const values = reactive<SignupValues>({ name: '', email: '', password: '' });
const touched = reactive({ name: false, email: false, password: false });
const showPassword = ref(false);
const errors = computed(() => ({
name: values.name.trim() ? '' : 'Enter your name.',
email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(values.email.trim()) ? '' : 'Enter an email like [email protected].',
password:
values.password.length >= props.minPassword ? '' : `Use at least ${props.minPassword} characters.`,
}));
type Field = keyof SignupValues;
function errorFor(field: Field): string {
return touched[field] ? errors.value[field] : '';
}
function onSubmit() {
touched.name = touched.email = touched.password = true;
const firstBad = (['name', 'email', 'password'] as const).find((f) => errors.value[f]);
if (firstBad) {
document.getElementById(`${id}-${firstBad}`)?.focus();
return;
}
emit('submit', { name: values.name.trim(), email: values.email.trim(), password: values.password });
}
</script>
<template>
<form class="signup" novalidate :aria-busy="pending" @submit.prevent="onSubmit">
<h2 class="title">Create your account</h2>
<div class="field">
<label :for="`${id}-name`">Name</label>
<input
:id="`${id}-name`"
v-model="values.name"
type="text"
autocomplete="name"
:aria-invalid="!!errorFor('name')"
:aria-describedby="errorFor('name') ? `${id}-name-error` : undefined"
@blur="touched.name = true"
/>
<p v-if="errorFor('name')" :id="`${id}-name-error`" class="error">{{ errorFor('name') }}</p>
</div>
<div class="field">
<label :for="`${id}-email`">Email</label>
<input
:id="`${id}-email`"
v-model="values.email"
type="email"
inputmode="email"This preview is shortened. You get the full content when you install it.
Changes in 1.0.0
First version.