rn-credit-card-textinput
v1.1.7
Published
React Native card-number inputs with legacy network and Luhn checks, plus date formatting
Maintainers
Readme
rn-credit-card-textinput
React Native card-number and date text inputs with customizable styles. The number input checks legacy network prefixes, lengths and the Luhn checksum and displays a card image or validation message. The date input formats text; it does not validate expiry dates.
Installation and compatibility
npm install rn-credit-card-textinputDeclared peers are react: ~17.0.1, react-dom: ~17.0.1, and react-native: 0.67.1. There are no runtime dependencies. These historical declarations are compatibility assumptions, not a tested platform matrix.
The current implementation was exercised from a generated npm tarball on an iPhone 17 / iOS 26.2 simulator in a temporary React Native 0.87.1 / React 19.2.3 app. This does not widen peer requirements or establish broad modern React Native support. Android execution, physical devices, Expo, react-native-web, and native execution on the declared React Native version remain unverified.
Release status: this README describes the current repository. Its version remains 1.1.6, but Phase 2–5 corrections have not been published. Installing the existing npm 1.1.6 release does not provide these corrections.
Controlled usage
import React, {useState} from 'react';
import {View} from 'react-native';
import {CardNumberTextInput, CardDateTextInput} from 'rn-credit-card-textinput';
export function CardFields() {
const [number, setNumber] = useState('');
const [date, setDate] = useState('');
return (
<View>
<CardNumberTextInput label="Card number" placeholder="Card number"
value={number} updateTextVal={setNumber} />
<CardDateTextInput label="Date (formatting only)" placeholder="MM/YY"
value={date} updateCardDateText={setDate} />
</View>
);
}value is displayed verbatim. Store callback output in parent state to display formatted text. Programmatic updates do not invoke callbacks or automatically format the supplied value. Number icons and messages follow the accepted parent value. If the parent declines an edit, its existing value remains authoritative. An initially empty number field is quiet; clearing after an edit shows No card number provided.
For uncontrolled usage, omit value and optionally supply defaultValue:
<CardNumberTextInput defaultValue="4111" updateTextVal={(text) => {
// Use formatted text in your application without controlling native text.
}} />The native field owns uncontrolled text. Callbacks return formatted text but do not replace its display. Initial defaultValue does not initialize number validation or a brand image. Avoid switching modes; use either value or defaultValue, rather than both.
Migration: published 1.1.6 ignored value. The current implementation honors it, so a constant or stale value holds the field at that text. Update it through the existing callback or omit it for uncontrolled usage. The cross-card state leak is corrected: validation no longer depends on prior calls. Fully reload an active development app when replacing the old implementation; Fast Refresh can retain incompatible hook state.
In controlled mode, Backspace at a generated separator removes the preceding digit too: 4111 becomes 411, and 12/ becomes 1. This requires the native Backspace event; existing onKeyPress is still forwarded. Ordinary typing and paste retain legacy formatting. Full cursor/selection and Android key-event behavior remain to be verified.
API and styling
The only root exports are CardNumberTextInput and CardDateTextInput.
| Component | Required callback | Output |
| --- | --- | --- |
| CardNumberTextInput | updateTextVal: (text: string) => void | Formatted number text |
| CardDateTextInput | updateCardDateText: (text: string) => void | Formatted date text |
Each callback runs once per native text change. Neither returns validity or brand information. Parent-only updates do not call them.
Both components extend React Native TextInputProps and expose these optional custom props:
| Prop | Type | Purpose |
| --- | --- | --- |
| inputStyle, labelStyle | StyleProp<TextStyle> | Input and label styles |
| inputWrapStyle, cardInputContainerStyle | StyleProp<ViewStyle> | Wrapper and outer container styles |
| label, placeholder | string | Display text |
| labelColor, errorColor | string | Text colors |
| focusColor, defaultBorderColor | string | Border colors |
| focus, touched | boolean | Parent-supplied presentation flags |
| value | string | Controlled native text |
| error | string | External date message; number uses its own validation message |
focus and touched are not automatically tracked. Use inherited onFocus and onBlur to manage them. Untouched borders use defaultBorderColor; touched error borders use the existing fixed red color. errorColor styles message text.
The components override onChangeText, native style, placeholder, keyboard type (number-pad), return-key options and clearButtonMode. Use the required callback and inputStyle instead of onChangeText and native style. Date also forces maxLength={5}. Other native props are forwarded; onKeyPress is wrapped for separator deletion and invokes the supplied handler.
Card-number rules and limitations
These are historical rules preserved for compatibility, not current network standards or comprehensive BIN/IIN coverage. Prefixes are literal starts-with matches, not issuer lookups.
| Brand | Prefixes | Configured lengths |
| --- | --- | --- |
| Visa | 4 | 13, 16 |
| MasterCard | 51–55 | 16 |
| DinersClub | 36, 38, 54, 55 | 14, 16 |
| CarteBlanche | 300–305 | 14 |
| AmEx | 34, 37 | 15 |
| Discover | 6011, 622, 64, 65 | 16 |
| JCB | 35 | 16 |
| enRoute | 2014, 2149 | 15 |
| Solo | 6334, 6767 | 16, 18, 19 |
| Switch | 4903, 4905, 4911, 4936, 564182, 633110, 6333, 6759 | 16, 18, 19 |
| Maestro | 5018, 5020, 5038, 6304, 6759, 6761, 6762, 6763 | 12*, 13, 14, 15, 16, 18, 19 |
| VisaElectron | 4026, 417500, 4508, 4844, 4913, 4917 | 16 |
| LaserCard | 6304, 6706, 6771, 6709 | 16, 17, 18, 19 |
*The global 13–19 digit gate prevents a configured 12-digit Maestro number from being accepted. All brands require Luhn, including legacy networks. Whitespace is removed for validation; remaining characters must be ASCII digits. Partial numbers and checksum failures show Credit card number is in invalid format. Unknown prefixes show Credit card number is invalid; matching prefixes with no valid length show Credit card number has an inappropriate number of digits.
For overlaps, the first prefix-and-length match in table order wins. A 16-digit 54/55 card is MasterCard, a matching VisaElectron is classified as Visa, and some Switch/Laser prefixes are classified under earlier brands. Images are provided for MasterCard, AmEx, Visa, Discover, VisaElectron, Maestro and Solo; other valid brands and invalid values use the generic image.
Formatting removes non-word characters and inserts a space after every four characters, including a trailing space at a multiple of four. Legacy letters and underscores survive formatting but fail validation. Formatting is not brand-specific and has no length cap. Controlled validation uses accepted parent text; uncontrolled validation uses raw native edits, so punctuation removed by formatting can produce different presentation in these modes.
Mastercard 2-series, Visa 19-digit support, new networks, and redesigned Discover/JCB/Maestro ranges are not implemented. An unexplained legacy single-number exception still emits a scam-warning message. Its provenance is unresolved; this is not a fraud-detection service or evidence about a cardholder. Checksum/prefix validation cannot establish issuance, ownership, funds, or payment authorization. There is no CVV validation or payment processing.
Date formatting
The date input applies legacy substitutions and allows digits and slashes, with a native five-character limit. Examples: 2 → 02/, 12 → 12/, 13 → 01/3, and 123 → 1/23. Programmatic value is displayed verbatim. It does not determine whether a month/year is valid or expired. Supply your own validation and pass error if required.
Distribution and development
The package retains main and react-native at dist/module/index.js, with types at dist/typescript/index.d.ts. It ships ESM-style JavaScript and assets, without a CommonJS build or an exports map. Root imports are the intended API; plain Node execution of React Native components is not promised.
Historical src/* and dist/* paths remain packed. Undocumented deep imports are potential compatibility concerns, not a permanent public API guarantee. Internal domain helpers are not root exports.
The repository's example/App.tsx demonstrates public imports and callbacks; it is a screen, not a standalone native application. No native example build scripts or projects are included.
For development, use npm with the committed lockfile:
npm ci
npm run checkcheck runs typecheck, lint, formatting checks, tests, build, packed-file checks, and TypeScript/bundler/Metro consumer verification. Additional commands include npm run test:watch, npm run build, npm run pack:check and npm run consumer:test. Do not include actual cardholder data in issues or tests. The repository records the implementation comparison in docs/compatibility-baseline.md and the pending clean final gate in docs/final-release-verification.md. Those internal documents are not packed. Per-phase checks do not establish release readiness.
