Frontend School

Address Form with Client-Side Validation

Machine Coding
easy

Address Form with Client-Side Validation

Easy
React
Forms
Validation
Frontend

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.

  1. 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.
  2. 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).
  3. 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.

  4. 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.

Constraints

Constraints & assumptions

  • You should use React for this problem. Functional components and Hooks are preferred.
  • The solution should be entirely client-side. No backend API integration is required.
  • Focus on basic HTML structure and CSS for layout. No complex styling libraries (e.g., Material-UI, Ant Design) are allowed unless explicitly part of your standard setup. Minimal custom CSS is expected.
  • No external form validation libraries (e.g., Formik, React Hook Form) should be used. Implement the validation logic yourself.
  • Assume the user's browser supports modern JavaScript features.

Out of scope

  • Server-side validation.
  • Complex state management solutions beyond useState.
  • Advanced UI/UX features like animations, debouncing validation, or complex accessibility features beyond basic label-input association.
  • Internationalization (i18n).

What interviewers evaluate

  • Correctness: Does the form function as specified, and are all validation rules correctly applied?
  • Code Structure & Readability: Is the code well-organized, easy to understand, and maintainable?
  • Error Handling: Are error messages clear, timely, and correctly displayed?
  • React Fundamentals: Appropriate use of state, props, and component lifecycle (though minimal for this problem).

Loading editor...