Overview
At Flipkart, ensuring accurate delivery information is crucial for a seamless customer experience. When users add a new address, we need to validate the provided details to prevent errors, reduce delivery exceptions, and maintain data integrity. This problem focuses on building a robust client-side form for adding a new address with real-time validation.
What you need to do / Task
Your primary task is to build a React component that renders an "Add New Address" form and implements client-side validation for its fields.
-
Form Structure: Create a form with the following input fields:
- Full Name: Text input
- Mobile Number: Text input
- Pincode: Text input
- Address Line 1: Text input
- Address Line 2 (Optional): Text input
- City: Text input
- State: Select/Dropdown (provide a few mock states)
- Address Type: Radio buttons (Home, Work)
- A "Submit" button.
-
Client-Side Validation: Implement validation rules for each field as follows:
- Full Name: Required; minimum 3 characters, maximum 50 characters.
- Mobile Number: Required; exactly 10 digits; must start with 6, 7, 8, or 9.
- Pincode: Required; exactly 6 digits.
- Address Line 1: Required; minimum 5 characters, maximum 100 characters.
- Address Line 2: Optional; maximum 100 characters.
- City: Required; minimum 2 characters, maximum 50 characters.
- State: Required (must select one option).
- Address Type: Required (must select one option).
-
Error Display: Display clear, user-friendly error messages next to each invalid field. Errors should appear when a field is touched/blurred and invalid, or on form submission if any field is invalid.
-
Form Submission Handling:
- If the form is valid, display a success message (e.g., an alert or a message below the form) and clear the form fields.
- If the form is invalid, prevent submission and display all relevant error messages.
UI/UX expectations
- The form should be reasonably laid out, with labels clearly associated with their input fields.
- Error messages should be visually distinct (e.g., red text) and placed close to the field they refer to.
- The submit button should trigger validation upon click.
- Basic accessibility: Use
for attributes for labels and id for inputs.
Acceptance criteria
- All specified form fields are present and correctly typed (text, select, radio).
- Each field's validation rule (required, length, format) is correctly implemented.
- Appropriate error messages are displayed for each invalid field.
- Error messages appear when a field is blurred and is invalid, or upon attempting to submit an invalid form.
- A successful submission clears the form and displays a success confirmation.
- An unsuccessful submission prevents default form behavior and highlights all validation errors.