Overview
In modern web applications, ensuring data integrity and providing a smooth user experience are paramount. Client-side form validation plays a crucial role in achieving this by catching user input errors early, reducing server load, and guiding users to provide correct information. This problem focuses on building a robust user profile settings form with comprehensive client-side validation.
What you need to do / Task
Your task is to implement a React component for a user profile settings form. The form should include several input fields, each with specific validation rules. You must ensure that the form provides immediate feedback to the user regarding invalid input and prevents submission until all required fields are correctly filled.
- Build the Form UI: Create a React component that renders a form with the following fields:
- Username: Text input.
- Email: Email input.
- Password: Password input.
- Confirm Password: Password input.
- Bio: Textarea (optional).
- Implement Client-Side Validation: Apply the following validation rules for each field:
- Username:
- Required.
- Minimum length: 3 characters.
- Maximum length: 20 characters.
- Must be alphanumeric (letters and numbers only).
- Email:
- Required.
- Must be a valid email format (e.g.,
user@example.com).
- Password:
- Required.
- Minimum length: 8 characters.
- Must contain at least one uppercase letter, one lowercase letter, one number, and one special character (e.g., !@#$%^&*).
- Confirm Password:
- Required.
- Must exactly match the
Password field.
- Bio:
- Optional.
- Maximum length: 150 characters.
- Display Validation Errors: Show clear and descriptive error messages directly below the corresponding input field when validation fails. Errors should appear on
blur events for individual fields and for all invalid fields on submit.
- Manage Form Submission: The submit button should be disabled if the form is in an invalid state (i.e., any required field has an error). Upon successful submission (all fields valid), log the form data to the console.
UI/UX expectations
- Layout: Arrange form fields vertically with clear labels for each input.
- Interactions: Input fields should update their values as the user types. Error messages should appear dynamically and disappear when the input becomes valid.
- Error Display: Error messages should be visually distinct (e.g., red text) and placed immediately below the input field they pertain to.
- Submit Button: The submit button's state (enabled/disabled) should clearly reflect the form's overall validity.
- Accessibility: Ensure labels are correctly associated with their inputs using
htmlFor and id. Provide aria-describedby for error messages to link them semantically to their inputs, and use aria-live="polite" on error containers for screen reader announcements.
Acceptance criteria
- The form renders all specified input fields (username, email, password, confirm password, bio) with appropriate labels.
- All validation rules for username, email, password, and confirm password are correctly implemented and enforced.
- The bio field correctly handles its optional status and maximum length constraint.
- Error messages are displayed accurately and immediately below the relevant input field when a field becomes invalid (on
blur or submit).
- Error messages disappear when the input is corrected and becomes valid.
- The 'Save Changes' button is disabled when any required field is invalid or empty, and enabled only when all fields are valid.
- Upon clicking 'Save Changes' with all valid inputs, the form data is logged to the console.
- Basic accessibility considerations (labels, error linking) are present.