Frontend School

Dynamic User Profile Form with Advanced Validation

Machine Coding
hard

Dynamic User Profile Form with Advanced Validation

Hard
Form Validation
React
TypeScript
UI/UX
Asynchronous Operations
State Management

Overview

Modern web applications demand robust and user-friendly forms to ensure data integrity and a smooth user experience. For a new internal tool managing sensitive user profiles, we need a multi-step form that goes beyond basic validation. This problem challenges you to build a dynamic form capable of handling various validation types, including asynchronous and cross-field checks, mimicking real-world complexities.

What you need to do / Task

Your goal is to implement a multi-step user profile form with comprehensive client-side validation. The form should guide the user through updating their 'Basic Info', 'Security Settings', and 'Contact Preferences'.

  1. Form Structure: Create a multi-step form with three distinct steps:

    • Step 1: Basic Info
      • username: Text input. Required, min 5 chars, max 20 chars, alphanumeric (letters and numbers only). Must include asynchronous validation to check for availability (simulate with a delay).
      • fullName: Text input. Required, min 3 chars, letters and spaces only.
      • email: Text input. Required, valid email format.
    • Step 2: Security Settings
      • password: Password input. Required, min 8 chars, at least one uppercase letter, one lowercase letter, one number, and one special character (e.g., !@#$%^&*).
      • confirmPassword: Password input. Required, must exactly match the password field.
    • Step 3: Contact Preferences
      • prefersEmailNotifications: Checkbox.
      • prefersSMSNotifications: Checkbox.
      • receivePhysicalMail: Checkbox.
      • shippingAddress: Text input. Dynamically required only if receivePhysicalMail is checked. Min 10 characters if required.
      • zipCode: Text input. Dynamically required only if receivePhysicalMail is checked. Must be exactly 5 digits if required.
  2. Client-Side Validation: Implement all specified validation rules for each field.

  3. Cross-Field Validation: Ensure confirmPassword matches password.

  4. Asynchronous Validation: Implement the username availability check with a simulated API call (e.g., setTimeout). Display a loading state during the check and prevent navigation/submission until the check is complete.

  5. Dynamic Validation: shippingAddress and zipCode fields should only be validated (and required) if receivePhysicalMail is checked.

  6. Error Display: Show clear, user-friendly error messages adjacent to the relevant input fields when validation fails.

  7. Navigation Control: The "Next" button should only be enabled when all fields in the current step are valid. The final "Submit" button should only be enabled when all fields across all steps are valid.

  8. Data Persistence: Form data should persist when navigating between steps (e.g., if a user goes back to a previous step, their entered data should still be there).

UI/UX expectations

  • Layout: A clean, multi-step layout with clear step indicators (e.g., "Step 1/3"). Fields within each step should be clearly labeled.
  • Interactions: Smooth transitions between steps. Input fields should provide immediate validation feedback on blur or change. Async validation should show a temporary loading indicator.
  • Accessibility: Consider basic accessibility for form elements, such as proper label association with inputs, and ARIA attributes for error messages (e.g., aria-invalid, aria-describedby).

Acceptance criteria

  • All specified client-side, cross-field, asynchronous, and dynamic validation rules are correctly applied.
  • The form correctly progresses through steps; "Next" is disabled if the current step is invalid.
  • Error messages are displayed accurately, are descriptive, and disappear when validation criteria are met.
  • Asynchronous validation for the username field includes a visible loading state and correctly blocks progression until complete.
  • Form data is preserved when navigating back and forth between steps.
  • The final "Submit" button is only enabled when the entire form (all steps) is valid.

Constraints

Constraints & assumptions

  • You must use React with TypeScript.
  • No external form libraries (e.g., Formik, React Hook Form, Zod) are allowed. Implement validation logic from scratch to demonstrate core understanding.
  • Simulate asynchronous validation (e.g., username availability) using setTimeout to mimic a network request (e.g., 500-800ms delay).
  • Focus on core functionality and robust validation logic; pixel-perfect styling is not required, but basic usability and clear error presentation are expected.
  • Assume a single user profile being edited; no need to handle multiple users or complex data fetching for initial state.

Out of scope

  • Backend integration beyond the mock async validation.
  • Advanced state management libraries (e.g., Redux, Zustand) – basic React state management (e.g., useState, useReducer, useRef) is sufficient and preferred.
  • Deep internationalization or complex theming.

What interviewers evaluate

  • Correctness and Robustness: Accuracy of all validation rules, handling of edge cases, and proper state transitions.
  • Architecture & Design: Clarity of component structure, separation of concerns, and effective state management for a complex form.
  • User Experience & Feedback: Clear, timely, and accessible feedback for validation errors, loading states, and navigation.
  • TypeScript Proficiency: Effective use of types for form data, validation errors, and component props.
  • React Idioms: Appropriate use of React hooks, component lifecycle, and rendering patterns.

Loading editor...