ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Type Safe Form Handling: User Registration

TSX

Goal -- WPM

Ready
Exercise Algorithm Area
1interface RegistrationFormValues {
2username: string;
3email: string;
4password: {
5value: string;
6confirm: string;
7};
8termsAccepted: boolean;
9}
10
11interface RegistrationErrors {
12username?: string;
13email?: string;
14password?: string;
15termsAccepted?: string;
16}
17
18function validateRegistrationForm(values: RegistrationFormValues): RegistrationErrors {
19const errors: RegistrationErrors = {};
20
21// Username validation
22if (!values.username) {
23errors.username = 'Username is required';
24} else if (values.username.length < 3) {
25errors.username = 'Username must be at least 3 characters';
26}
27
28// Email validation
29const emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/;
30if (!values.email) {
31errors.email = 'Email is required';
32} else if (!emailRegex.test(values.email)) {
33errors.email = 'Invalid email address';
34}
35
36// Password validation
37if (!values.password.value) {
38errors.password = 'Password is required';
39} else if (values.password.value.length < 6) {
40errors.password = 'Password must be at least 6 characters';
41} else if (values.password.value !== values.password.confirm) {
42errors.password = 'Passwords do not match';
43}
44
45// Terms acceptance validation
46if (!values.termsAccepted) {
47errors.termsAccepted = 'You must accept the terms';
48}
49
50return errors;
51}
52
53function handleRegistration(values: RegistrationFormValues): void {
54const errors = validateRegistrationForm(values);
55
56if (Object.keys(errors).length > 0) {
57console.error('Form validation failed:', errors);
58// In a real app, you would update state to display errors to the user.
59return;
60}
61
62console.log('Registration successful:', values.username, values.email);
63// Proceed with registration logic (e.g., API call)
64}
Algorithm description viewbox

Type Safe Form Handling: User Registration

Algorithm description:

This module provides type-safe handling for a user registration form. It defines interfaces for form values and validation errors, ensuring that all form fields and their potential error messages are clearly typed. The `validateRegistrationForm` function checks for required fields, minimum lengths, email format, password confirmation, and terms acceptance, returning a structured error object.

Algorithm explanation:

The `handleRegistration` and `validateRegistrationForm` functions enforce type safety in form handling. `RegistrationFormValues` defines the structure of the form data, and `RegistrationErrors` defines the structure of validation feedback. The `validateRegistrationForm` function iterates through defined rules, checking for common issues like missing fields, length constraints, and format validity. The use of a separate `errors` object allows for accumulating multiple validation messages. Time complexity is O(N) where N is the number of fields, as each field is checked once. Space complexity is O(M) where M is the number of fields with errors, for storing the error messages.

Pseudocode:

Define RegistrationFormValues interface with username, email, password (object with value/confirm), and termsAccepted (boolean).
Define RegistrationErrors interface with optional string properties for each form field.
Create validateRegistrationForm function that takes RegistrationFormValues and returns RegistrationErrors.
Initialize an empty errors object.
Validate username: check if empty or too short.
Validate email: check if empty or matches regex.
Validate password: check if empty, too short, or if value and confirm do not match.
Validate termsAccepted: check if false.
If any validation fails, add an error message to the errors object.
Return the errors object.
Create handleRegistration function that takes RegistrationFormValues.
Call validateRegistrationForm with the input values.
If the returned errors object has any keys:
  Log an error message with the errors.
  Return.
Log a success message with username and email.
(In a real app, proceed with submission.)