Skip to content

Interactive card details form - Fill in the form and see the card details update in real-time

Notifications You must be signed in to change notification settings

cindyeme/interactive-card-details-form

Repository files navigation

Frontend Mentor - Interactive card details form solution

This is a solution to the Interactive card details form challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.

Table of contents

Overview

The challenge

Users should be able to:

  • Fill in the form and see the card details update in real-time
  • Receive error messages when the form is submitted if:
    • Any input field is empty
    • The card number, expiry date, or CVC fields are in the wrong format
  • View the optimal layout depending on their device's screen size
  • See hover, active, and focus states for interactive elements on the page

Screenshot

Desktop-Design Active-States Complete-State-Desktop Mobile-Design Complete-State-Mobile

Links

My process

Built with

  • JSX
  • CSS custom properties
  • Flexbox
  • Mobile-first workflow
  • TailwindCSS - CSS framework
  • Next.js - React framework
  • Yup - For validations

What I learned

While working on this, I learned how to write logic for form validation, handle the form input changes when it got to splitting the card numbers into four separate digits, and how to find solutions and fix bugs as I encounter them.

See some code snippet below below:

 const handleInputChange = (e) => {
    if (e.target.name === "number" && e.target.value) {
      e.target.value = e.target.value
        .replace(/\s/g, "")
        .replace(/(.{4})/g, "$1 ")
        .trim()
        .slice(0, 19);
    }

    if (e.target.name === "expiryMonth" || e.target.name === "expiryYear") {
      e.target.value = e.target.value
        .toString()
        .replace(/[^0-9]/g, "")
        .substring(0, 2);
      if (e.target.name === "expiryMonth" && e.target.value > 12)
        e.target.value = "12";
    }

    if (e.target.name === "cvc") {
      e.target.value = e.target.value.substring(0, 3);
    }
    setFormData({ ...formData, [e.target.name]: e.target.value });
  };
const handleSubmit = async (e) => {
  e.preventDefault();
  const isFormValid = await validationSchema.isValid(formData, {
    abortEarly: false,
  });

  if (isFormValid) {
    setValidate(true);
    setErrors({
      name: "",
      number: "",
      expiryMonth: "",
      expiryYear: "",
      cvc: "",
    });
  } else {
    validationSchema
      .validate(formData, { abortEarly: false })
      .catch((error) => {
        const errors = error.inner.reduce((acc, error) => {
          return {
            ...acc,
            [error.path]: error.message,
          };
        }, {});
        setErrors({
          name: errors.name,
          number: errors.number,
          expiryMonth: errors.expiryMonth,
          expiryYear: errors.expiryYear,
          cvc: errors.cvc,
        });
      });
    }
  };

Continued development

I would be using more of React.js and Next.js frameworks in the coming challenges and would enjoy implementing logics.

Author

About

Interactive card details form - Fill in the form and see the card details update in real-time

Topics

Resources

Stars

Watchers

Forks