A comprehensive credit card validation library for Swift built on the Luhn algorithm. It validates card numbers, detects the issuing network from the prefix, formats and masks numbers for display, validates expiry dates and CVV codes, and exposes ergonomic String and Array extensions โ supporting all major networks including Visa, Mastercard (2-series), Amex, Discover, Diners Club, JCB, UnionPay, and Maestro.
- โ
Luhn validation โ
validatechecks length, type-specific length, and checksum - ๐ฆ Network detection โ
detectTypeidentifies the card network from its prefix - ๐ฏ 8 card networks โ Visa, Mastercard, Amex, Discover, Diners Club, JCB, UnionPay, Maestro
- ๐ข Formatting โ
formatgroups digits per network (e.g.XXXX XXXX XXXX XXXX) - ๐ Masking โ
maskshows only the last four digits with a configurable mask character - ๐
Expiry validation โ
validateExpiryaccepts MM/YY and MM/YYYY and rejects past dates - ๐ CVV validation โ
validateCVVenforces the correct length per network (4 for Amex) - ๐งช Test numbers โ
generateTestNumber(for:)returns documented Luhn-valid test cards - ๐ค String extensions โ
isValidCreditCard,creditCardType,formattedCreditCard,maskedCreditCard,creditCardInfo, and more - ๐ Array extensions โ filter, group, and mask collections of card numbers
- ๐งต Sendable models โ value types ready for concurrent use
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
- Swift 6.1+
- Xcode 16.0+
dependencies: [
.package(url: "https://github.com/arraypress/swift-credit-card-validator.git", from: "1.0.0")
]import CreditCardValidator
let isValid = CreditCardValidator.validate("4532015112830366") // true
let type = CreditCardValidator.detectType("4532015112830366") // .visaimport CreditCardValidator
let formatted = CreditCardValidator.format("4532015112830366", type: .visa)
// "4532 0151 1283 0366"
let masked = CreditCardValidator.mask("4532015112830366", type: .visa)
// "**** **** **** 0366"import CreditCardValidator
let expiryOK = CreditCardValidator.validateExpiry("12/2030") // true
let cvvOK = CreditCardValidator.validateCVV("1234", type: .amex) // true (Amex requires 4)import CreditCardValidator
let testVisa = CreditCardValidator.generateTestNumber(for: .visa)
// "4532015112830366" โ for testing onlyimport CreditCardValidator
let valid = "4532 0151 1283 0366".isValidCreditCard // true
let network = "4532015112830366".creditCardType // .visa
let pretty = "4532015112830366".formattedCreditCard // "4532 0151 1283 0366"
let hidden = "4532015112830366".maskedCreditCard // "**** **** **** 0366"
let info = "4532015112830366".creditCardInfo // CreditCardInfo?
let cards = ["4532015112830366", "5425233430109903", "invalid"]
let onlyValid = cards.validCreditCards
let grouped = cards.groupedByCardType
let maskedAll = cards.maskedCreditCardsvalidate strips non-digits, enforces a 12โ19 digit range, detects the network from the prefix (including Mastercard's 2221โ2720 2-series and extended Discover/Maestro ranges), checks that the length is valid for that network, and finally runs the Luhn checksum. Formatting and masking use each network's grouping pattern, and CVV/expiry validation apply network-specific and calendar-aware rules.
| Property | Type | Description |
|---|---|---|
number |
String |
Digits-only card number |
type |
CardType |
Detected card network |
isValid |
Bool |
Whether the number passes validation |
lastFourDigits |
String |
Last four digits |
firstSixDigits |
String |
First six digits (BIN) |
length |
Int |
Number of digits |
CardType (.visa, .mastercard, .amex, .discover, .dinersClub, .jcb, .unionPay, .maestro) exposes validLengths, cvvLength, and formatPattern.
- Checkout and payment form validation
- Card-on-file display with safe masking
- Network-aware formatting in UI
- Test-data generation for development
swift testThe test suite covers Luhn validation, network detection across all eight card types, formatting, masking, expiry and CVV validation, the test-number generator, and the String/Array convenience extensions.
MIT License โ see LICENSE file for details.
Created by David Sherlock (ArrayPress) in 2026.