The top pick is a layered card verification pattern set: per-brand format patterns, a Luhn checksum gate, and a BIN lookup evaluated in sequence, instead of one large regular expression. The criteria that matter here are coverage of the brands you actually accept, false-positive behavior at checkout, maintainability when brands change their ranges, and how easily the rules can be tested with synthetic numbers. Everything below assumes you are validating numbers your own customers type into your own form, using documented test ranges rather than live account data.
credit card verification scheme
What a pattern set has to describe
A card verification pattern set is the collection of rules that decides whether an entered card number, expiry date, and security code are structurally plausible before you send an authorization request. It usually covers four things: the primary account number format per brand and length, the check digit, the expiry date in MM/YY form with a month between 01 and 12, and the security code length. It does not tell you whether the card is funded or valid; only the issuer does that.
Option 1: one combined regular expression
A single expression that accepts every brand at once, such as a leading 4 followed by 12, 15, or 18 digits for Visa, plus alternated branches for the other ranges.
Pros
- Fast to write and fast to run on the client side.
- One rule to ship in a form validation library.
Cons
- Brand lengths overlap, so the expression becomes a long alternation that is hard to audit.
- It cannot tell you which brand matched, so you cannot adapt the security code field length.
- Updating one brand range risks breaking another branch.
Use it when you accept a single brand or when the pattern only needs to gate obviously malformed input.
Card Check Routine: How to Verify a Payment Card in 5 Steps
Option 2: layered pattern set
Format patterns run first, per brand, and return a brand label. A Luhn check runs second. A BIN table supplies issuer country and card type third. Each layer has one job and one failure message.
Pros
- The brand label lets the form switch the security code field between three and four digits, which matters for American Express.
- Each layer is testable in isolation, so a failing rule points to a specific cause.
- Checksum rejection catches most typos before you spend an authorization attempt.
Cons
- More code and more configuration than a single expression.
- BIN data goes stale and needs a refresh schedule.
Use it for any multi-brand checkout, and especially where declines from mistyped numbers carry a real cost.
Option 3: BIN-table-driven validation
Instead of hardcoding ranges, you load an issuer table and match the longest prefix. Ranges, lengths, and security code rules all come from data.
Pros
- Range changes become a data update rather than a code release.
- You get issuer metadata for routing and risk rules at no extra parsing cost.
Cons
- Requires a licensed or maintained data source and a versioning plan.
- Overkill for a small merchant with one acquiring bank.
Use it if you process across many regions or need issuer-level routing decisions.
Testing the set
Build a fixture file with one row per brand and per expected failure: a valid length with a bad check digit, a correct prefix with a wrong length, month 13, an expired date, and a three-digit code on a four-digit brand. Use the publicly documented test card numbers published by processors for sandbox runs. Add a regression test that fails when a brand range changes without a matching fixture update.
Storage and logging limits
PCI DSS requires that the card verification value never be stored after authorization, even in encrypted form, so the pattern set should validate the code and then discard it. Mask the account number in logs to the last four digits, and keep the full number out of application logs, error trackers, and analytics events.