Giter Club home page Giter Club logo

step-form's Introduction

pt-br ๐Ÿ‡ง๐Ÿ‡ท

๐Ÿ› Step Form

An abstraction of a form commonly used in ecommerce, with the steps of personal data, address and payment method (involving only card payments). ๐Ÿ›’

Subjects โ€ข Demonstration โ€ข Structure โ€ข Validations โ€ข Achievements, difficulties and improvements

๐Ÿ“š Subjects

  • HTML
  • CSS
  • JavaScript (Arrays, Objects, DOM and Fetch API)
  • Regex (Capturing groups, replace method and its use, backreferences)

๐Ÿ–ฅ๏ธ Demonstration

Try it here. step-form

๐Ÿ—๏ธ Structure

    โ”œโ”€โ”€ assets
    โ”‚    โ”œโ”€โ”€ flags
    โ”‚    โ”œโ”€โ”€ icons
    โ”‚    โ””โ”€โ”€ images
    โ”‚
    โ”œโ”€โ”€ css
    โ”‚    โ”œโ”€โ”€ components
    โ”‚    โ”‚     โ”œโ”€โ”€ button.css
    โ”‚    โ”‚     โ”œโ”€โ”€ calendar.css
    โ”‚    โ”‚     โ”œโ”€โ”€ input.css
    โ”‚    โ”‚     โ”œโ”€โ”€ label.css
    โ”‚    โ”‚     โ””โ”€โ”€ stepper.css
    โ”‚    โ”‚
    โ”‚    โ”œโ”€โ”€ form.css
    โ”‚    โ”œโ”€โ”€ global.css
    โ”‚    โ”œโ”€โ”€ payment.css
    โ”‚    โ”œโ”€โ”€ resume.css
    โ”‚    โ””โ”€โ”€ root.css
    โ”‚
    โ”œโ”€โ”€ js
    โ”‚    โ”œโ”€โ”€ components
    โ”‚    โ”‚     โ”œโ”€โ”€ calendar.js
    โ”‚    โ”‚     โ”œโ”€โ”€ form
    โ”‚    โ”‚     โ””โ”€โ”€ stepper.js
    โ”‚    โ”‚
    โ”‚    โ”œโ”€โ”€ pages
    โ”‚    โ”‚     โ”œโ”€โ”€ address.js
    โ”‚    โ”‚     โ”œโ”€โ”€ payment.js
    โ”‚    โ”‚     โ”œโ”€โ”€ personal-data.js
    โ”‚    โ”‚     โ””โ”€โ”€ resume.js
    โ”‚    โ”‚
    โ”‚    โ”œโ”€โ”€ utils
    โ”‚    โ”‚     โ”œโ”€โ”€ create-element.js
    โ”‚    โ”‚     โ””โ”€โ”€ formatter.js
    โ”‚    โ”‚
    โ”‚    โ””โ”€โ”€ validations
    โ”‚          โ”œโ”€โ”€ card-flag.js
    โ”‚          โ”œโ”€โ”€ date.js
    โ”‚          โ”œโ”€โ”€ input-mask.js
    โ”‚          โ””โ”€โ”€ inputs.js
    โ”‚
    โ”œโ”€โ”€ pages
    โ”‚    โ”œโ”€โ”€ address.html
    โ”‚    โ”œโ”€โ”€ payment.html
    โ”‚    โ””โ”€โ”€ resume.html
    โ”‚
    โ”œโ”€โ”€ index.html
    โ”œโ”€โ”€ LICENSE
    โ”œโ”€โ”€ package.json
    โ””โ”€โ”€ README.md

๐Ÿ“‹ Validations

Most of the validations were done using custom functions and regex masks in the inputs, while the user types (onInput) or when the input loses focus (onChange) for a more immediate response, optimizing the user's time, facilitating and limiting what is entered in a simple, visually intuitive and pleasant way, in addition to ensuring greater tolerance to errors in the fields entered.

The onInput and onChange events are similar, but the main difference is that onInput occurs immediately after the input value changes, while onChange occurs when the input loses focus. Another difference is that onChange also works on <select> elements.

Input masks

For most masks, the implementation was based on the concept of regex capturing groups along with the .replace() method which, at each value change, checks and, when necessary, add characteristic symbols using the temporary variables that are created and enumerated according to the order of opening parentheses in the expression.

Explaining better, capture groups are constructed by placing the pattern to be captured in parentheses. Example:

const phone = (value) => {
  return value
    .replace(/\D/g, "")
    .replace(/(\d{2})(\d)/, "($1) $2")
    .replace(/(\d{5})(\d)/, "$1-$2")
    .replace(/-(\d{4})(\d)/, "$1");
};

The substring corresponding to the group is saved in a temporary "variable", which can be accessed within the same regex using a backslash and the capturing group number, such as /(\d) \1/ or accessing through the notation $(group number), which is used in the replace method. Remembering that the enumeration is according to the position of its opening parentheses (from left to right), starting at 1:

  .replace(/(\d{2})(\d)/, "($1) $2")

This means that we want the first alphanumeric with size equal to 2 to be placed in parentheses and the next value entered to be separated from the first group with a space. We call these backreferences.

Card brands

Regarding the method to validate and identify the card brand that I chose to follow, I inform you that it is not very recommended, as it is based on validating the card's BIN (the first 6 digits of the card number) in regex and BINs may change over the years. Furthermore, working with regex on this type of variable data is not scalable or easy to maintain.

Before you ask, I chose this method because it is independent of other services and, consequently, will not present future unavailability, despite possible incompatibilities that may occur in the available brands.

But for those who are interested in the services, I found some APIs that offer free tier:

API Limitation
BIN Codes Registration required and limit of 20 requests per day
BINLIST.NET No need to register, but it has stopped being updated (2023) and has a limit of 10 requests per minute
bincheck.io Registration required and limit of 1000 requests per month

๐Ÿ‘ฉโ€๐Ÿ’ป Achievements, difficulties and improvements

  • โ˜€๏ธ Achievements:

    • Project structure organization;
    • Providing valuable error feedback to the user;
    • Limiting user error margins through the use of masks;
    • Creative solution for "simulating states" using only pure JavaScript.
  • ๐ŸŒง๏ธ Difficulties:

    • Control error states for the inputs;
    • Establish a well-organized structure for the scripts and CSS folder;
    • Apply the Single Responsibility concept from SOLID;
    • Find reliable information to validate payment-related fields;
    • Implement regex to create masks for the inputs and search for information on how to implement it using pure JavaScript in a visually pleasing way for the fields.
  • ๐ŸŒˆ Improvements:

    • When the user goes back a step, ensure that the filled information remains in the fields;
    • When the user leaves the page or clicks to go back, notify that the data from the current step will be lost before proceeding;
    • Enhance validation accuracy by utilizing one of the APIs mentioned in the Validation section in the Card brands.

Made with ๐Ÿ’œ by Ana Beatriz Nunes

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.