Dynamic User Profile Form with Advanced Validation
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.
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'.
Form Structure: Create a multi-step form with three distinct steps:
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.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.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.Client-Side Validation: Implement all specified validation rules for each field.
Cross-Field Validation: Ensure confirmPassword matches password.
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.
Dynamic Validation: shippingAddress and zipCode fields should only be validated (and required) if receivePhysicalMail is checked.
Error Display: Show clear, user-friendly error messages adjacent to the relevant input fields when validation fails.
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.
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).
label association with inputs, and ARIA attributes for error messages (e.g., aria-invalid, aria-describedby).Constraints
setTimeout to mimic a network request (e.g., 500-800ms delay).useState, useReducer, useRef) is sufficient and preferred.Loading editor...