Skip to content

Luhn validation and card types for card numbers

Notifications You must be signed in to change notification settings

LootApp/Plastic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plastic

Luhn validation and card types for credit/debit card numbers

Platform: iOS 8+


Here are some useful resources on the Luhn algorithm:

https://en.wikipedia.org/wiki/Luhn_algorithm
https://www.rosettacode.org/wiki/Luhn_test_of_credit_card_numbers


Thank you to Max Kramer for the initial inspiration.



Features

  • Check the type of a card number (Amex, DinersClub, Discover, JCB, MasterCard or Visa)
  • Check if a given card number is valid

Usage

let cardNumber = "3782 822463 10005"
let isValid = cardNumber.plastic_luhnValidate()
do {
    print(try cardNumber.plastic_cardType()) // Prints "Amex" in the console
} catch {
    // Handle error
    // Only error that Plastic throws is PlasticError.InvalidCardNumber
    // Thrown if the card number is < 9 digits and not Luhn valid
}

Tests

Test card numbers are from PayPal.