card-validator
Validate credit card fields as users type, with potential-validity states and card-type detection.
Repository Health
Technical Analysis
card-validator is a lightweight, framework-agnostic JavaScript library from Braintree for validating credit card form inputs. It validates card numbers, expiration dates, CVV, cardholder names, and postal codes, returning both strict validity and a distinctive isPotentiallyValid flag so you can drive responsive form UI while the user is still typing.
The headline feature is that notion of ‘potential’ validity: a partially typed number like “411” is not yet valid for submission but is still potentially valid, whereas “41x” can be rejected immediately. Each number validation also returns the detected card brand (via credit-card-type), letting you render the right payment icon and adjust CVV length and labels dynamically.
What You Get
- Validators for card number, expiration date/month/year, CVV, cardholder name, and postal code
- An isPotentiallyValid flag for real-time, forgiving form feedback
- Automatic card-brand detection with gaps, lengths, and CVV size metadata
- Framework-agnostic pure functions usable in Node.js or any browser bundle
- First-class TypeScript type definitions
Common Use Cases
- Giving live, non-punishing validation feedback in a credit card checkout form
- Detecting the card brand to show the correct icon and adjust CVV length/label
- Blocking obviously invalid input while allowing still-in-progress entries
- Server-side sanity checks on card field shape before tokenization
Under The Hood
Architecture - The library is organized as one focused module per field under src/ (card-number.ts, expiration-date.ts, cvv.ts, cardholder-name.ts, postal-code.ts, etc.), each exporting a pure function that returns a verification object. card-number.ts delegates brand detection to the sibling credit-card-type package and applies a Luhn-10 checksum (luhn-10.js) plus length rules to compute strict and potential validity. index.ts re-exports the whole surface.
Tech Stack - Written in TypeScript, compiled with tsc --declaration to a CommonJS dist/. Its single runtime dependency is credit-card-type ^10; everything else (Jest, ts-jest, ESLint, Prettier, Husky, commitlint) is dev tooling. Targets Node.js and browsers via bundlers.
Code Quality - Strong: each field validator has a dedicated Jest test file under src/__tests__/, linting runs automatically as a posttest step, and commits are governed by commitlint/commitizen. The code is small, typed, and side-effect-free, which makes behavior easy to reason about and test.
API Design - Excellent developer experience. Every function follows the same { isValid, isPotentiallyValid } contract (with number additionally returning card metadata), so the API is consistent and predictable. The potential-validity concept is a thoughtful design that maps directly onto good form UX, and TypeScript types make the return shapes self-documenting.