explaingit

nosir/cleave.js

17,875JavaScriptAudience · developerComplexity · 2/5Setup · easy

TLDR

A small JavaScript library that formats text inside a form input as the user types, automatically inserting spaces, dashes, and slashes to make credit card numbers, phone numbers, and dates easy to read.

Mindmap

mindmap
  root((repo))
    What it formats
      Credit card numbers
      Phone numbers
      Dates
      Custom patterns
    How it works
      Wraps a text input
      Rewrites on keystroke
      Raw value still readable
    Integration options
      Plain script tag
      React component
      AngularJS directive
      ES Module
    Status
      Deprecated by author
      Successor is cleave-zen
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Add automatic credit card formatting with spaces every four digits to a checkout page without writing custom key-event logic.

USE CASE 2

Format a phone number input in real time with country code and dashes so users can see they are entering it correctly as they type.

USE CASE 3

Apply a custom delimiter pattern to an ID or code input so long strings stay scannable without any validation logic on the client side.

Tech stack

JavaScriptES ModuleCommonJSAMDReactAngularJS

Getting it running

Difficulty · easy Time to first run · 5min

This project has been deprecated by its author, the README points to the successor library cleave-zen for new projects.

In plain English

Cleave.js is a small JavaScript helper that formats text inside an input field as the user types. Instead of writing complicated rules to insert spaces, dashes, or slashes into things like credit card numbers, phone numbers, dates, or large numerals, you point Cleave at an input element and tell it what kind of value it is meant to hold. The library then keeps the visible text neatly grouped and readable while the user is still typing, which makes long sequences of digits much less error-prone. It works by wrapping a normal text input. You give it a CSS selector for the field and an options object describing the format, such as credit card mode, phone mode with a country code, or a custom pattern of delimiters, prefixes, and digit blocks. As keystrokes happen, Cleave rewrites the displayed value to match the chosen format, while still letting you read out the raw value when you need to send data to a backend. The README is explicit that Cleave is about readability, not validation, so checks for whether a card or phone number is actually valid still belong on the server. You would reach for it when building forms where typed input is hard to scan, like checkout pages, signup flows, or admin tools that capture identifiers. It ships in several flavors so it can drop into different setups: a plain script tag, CommonJS, AMD, an ES Module build, a ReactJS component, and an AngularJS 1.x directive, with community TypeScript types available separately. The README also notes the project has been deprecated by its author, who points users to a successor project called cleave-zen.

Copy-paste prompts

Prompt 1
Using Cleave.js, add live credit card formatting to this HTML input element so it shows spaces every four digits and still lets me read the raw value on form submit.
Prompt 2
I have a React checkout form. Show me how to use the Cleave.js React component to format a card number, expiry date, and CVV field as the user types.
Prompt 3
I need a phone input for US numbers using Cleave.js. Give me the options object and show me how to retrieve the unformatted digits before sending to the server.
Open on GitHub → Explain another repo

← nosir on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.