Configs2026年7月21日·1 分钟阅读

Cleave.js — Format Input Text as You Type

A lightweight JavaScript library that automatically formats credit card numbers, phone numbers, dates, and custom patterns in real time as the user types into an input field.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Cleave.js Overview
先审查命令
npx -y tokrepo@latest install cd74d8b2-8547-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

Cleave.js solves the common UX problem of formatting user input in real time. Instead of validating and reformatting after submission, it formats credit card numbers, phone numbers, dates, numerals, and custom patterns as the user types. This gives users immediate visual feedback, reduces errors, and eliminates the need for separate formatting logic.

What Cleave.js Does

  • Formats credit card numbers with automatic card type detection (Visa, Mastercard, Amex, etc.)
  • Formats phone numbers by region using Google's libphonenumber
  • Formats dates with configurable patterns like MM/DD/YYYY or YYYY-MM-DD
  • Formats numerals with thousands separators, decimal scales, and prefix/suffix
  • Supports custom delimiters and block patterns for arbitrary formatting

Architecture Overview

Cleave.js attaches to an input element and intercepts keystrokes via input event listeners. It maintains an internal formatter that applies the configured pattern rules to the raw value, calculating cursor position to keep the caret in the correct location after reformatting. The library is written in vanilla JavaScript with no dependencies and is distributed as both CommonJS and ES modules. React and Angular wrappers are provided as separate packages.

Self-Hosting & Configuration

  • Install via npm or include the script tag from a CDN
  • Create a new Cleave instance passing a CSS selector and an options object
  • For credit cards, set creditCard: true and listen to onCreditCardTypeChanged
  • For phone numbers, import the phone addon for your region and set phone: true
  • For custom patterns, use blocks, delimiters, and numericOnly options

Key Features

  • Automatic credit card type detection with brand-specific formatting
  • Phone formatting powered by Google libphonenumber for 200+ regions
  • Date formatting with min/max date validation
  • Numeral formatting with configurable thousands separator and decimal mark
  • Raw value access via getRawValue() for form submission without formatting characters

Comparison with Similar Tools

  • Inputmask — more comprehensive masking library with regex support; Cleave.js focuses on common patterns with a simpler API
  • IMask — flexible masking with pipe and pattern support; Cleave.js is more opinionated and quicker to set up
  • vanilla-masker — lightweight alternative but less maintained
  • Native pattern attribute — only validates on submit; Cleave.js formats in real time

FAQ

Q: How do I get the unformatted value for form submission? A: Call cleaveInstance.getRawValue() to retrieve the value without delimiters and formatting characters.

Q: Does it work with React? A: Yes. Install cleave.js and use the provided React component import Cleave from 'cleave.js/react', or use it with refs in a useEffect hook.

Q: Can I customize the formatting pattern? A: Yes. Use the blocks array to define segment lengths and delimiters to specify separator characters between blocks.

Q: Does it handle paste events? A: Yes. Pasted text is automatically reformatted according to the configured pattern.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产