Luhn Credit Card Validation Algorithm

Overview

Luhn Algorithm

Circle CI CocoaPods

This is a port of the Luhn Algorithm, generally used for validating Credit Card details, to Objective-C (iOS).

Swift port can be found at https://github.com/MaxKramer/SwiftLuhn.

It's as simple as calling one single method; no special formatting is required.

You must run pod install before opening the Example Project's 'workspace.

Instructions

Import the header:

#import "Luhn.h"

Call the class method returning a BOOL as to whether the string is valid or not:

BOOL isValid = [Luhn validateString:@"some credit card number"];
    
if (isValid) {
    // process payment   
}
else {
    // alert user
}

Alternatively, you can use the NSString category (no additional imports required):

NSString *ccNumber = @"378282246310005";
BOOL isValid = [ccNumber isValidCreditCardNumber];

You can also get the type of the credit card i.e. Visa, Diners, Amex, etc:

OLCreditCardType cardType = [ccNumber creditCardType];
// or
OLCreditCardType cardType = [Luhn typeFromString:ccNumber];

If you come across any issues or have a feature request, please open an issue or find me on:

Twitter: @maxkramer

Website: http://maxkramer.co

Sources

Name Website Reason
Paypal http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm List of valid credit card numbers for the unit tests

Thanks

Ayaka Nonaka for updating the Cocoapod spec and helping me with other aspects of the project!

Twitter: @ayanonagon

License

This project is licensed under the MIT License.

Comments
  • Reorganize so that the example project consumes the pod using a Podfile.

    Reorganize so that the example project consumes the pod using a Podfile.

    This is better form so you don't need to have two copies each of Luhn.h and Luhn.m. All you need to do now is run pod install from the example project directory as if it were a third party app consuming it. Let me know if you have any questions.

    Just wanted to make this PR first cause I wanted to add some tests for it after seeing someone use this pod. Love that you wrote the Luhn algorithm in Objective-C. :octocat: :thumbsup:

    Cheers.

    opened by ayanonagon 5
  • Make example project consume pod via Podfile again.

    Make example project consume pod via Podfile again.

    I know this looks like a big PR, but all I did is remove the duplicate Luhn.h and Luhn.m in the example project and move its contents to the other copies in Algorithm Class which is where the podspec is looking for the files. (So the new stuff you wrote today is in now in the Algorithm Class directory.)

    You don't need to worry about keeping the old copies of the Luhn classes because Cocoapods uses git tags to keep track of which version to use.

    When you pull this, make sure to run pod install from the Example Project directory. I've made sure the example app and tests run.

    Let me know if you have any questions!

    :octocat:

    opened by ayanonagon 3
  • Allow passing nil and short strings to +validateString:

    Allow passing nil and short strings to +validateString:

    I want to validate the credit card number in a UITextField every time the user types a character, so that I can immediately provide them with feedback as soon as they correct any errors. Currently, this fails the assert when the user begins to type in an empty text field (because the length is <9). It seems reasonable to return NO for a string that is too short or nil. If I'm not mistaken, NSAssert is usually used to catch programmer errors, which does not seem to apply to this use case.

    opened by dmdeller 2
  • Add prefixes to category method names

    Add prefixes to category method names

    It's often recommended (although far from widely agreed upon) to prefix category method names to protect against name collisions. I'm not sure if the absence of prefixes in the existing code was intentional or not, so if it was, please feel free to ignore this PR. I made the change for myself anyway, so I figured I may as well offer it to you.

    opened by dmdeller 1
  • Make style more like standard Objective-C.

    Make style more like standard Objective-C.

    Also makes it 64 bit friendly by using NSUInteger instead of unsigned int. NSUInteger is defined as unsigned long under 64 bit.

    screen shot 2014-02-24 at 9 26 29 pm

    I mostly want to test out CI :octocat: :thumbsup:

    opened by ayanonagon 1
  • Cocoapods old version

    Cocoapods old version

    Cocoapods installing old version without OLCardType.

    pod 'ObjectiveLuhn', '~> 1.0.1'

    It's not getting the package, we can only install 1.0 version.

    opened by mehmettaskiner 0
  • Format input string

    Format input string

    Input string needs to be stripped of spaces and some form of validation should be taken. As characters are used, the ascii value of '-' will be added as an integer to the odd/even number and could affect results. '-' should also be stripped.

    bug 
    opened by maxkramer 0
  • Newcard/maestro

    Newcard/maestro

    Added a regex to validate MAESTRO card

    ^(5018|5020|5038|6304|6759|676[1-3]|6220)[0-9]{12}([0-9]{2}|[0-9]{3}|[0-9]{0})$

    Eg: 6763269232330618 5038188028168894222

    enhancement 
    opened by gauravkeshre 3
Owner
Max Kramer
Engineering Leader // ex-BCG DV
Max Kramer
Luhn Credit Card Validation Algorithm

Luhn Algorithm This is a port of the Luhn Algorithm, generally used for validating Credit Card details, to Objective-C (iOS). Swift port can be found

Max Kramer 127 Nov 27, 2022
card.io provides fast, easy credit card scanning in mobile apps

card.io SDK for iOS card.io provides fast, easy credit card scanning in mobile apps. NEW!!! card.io is now an open-source project! As of December 2014

card.io 2.3k Jan 4, 2023
Easy to use iOS library with components for input of Credit Card data.

AnimatedCardInput This library allows you to drop into your project two easily customisable, animated components that will make input of Credit Card i

Netguru 39 Oct 16, 2022
Easily integrate Credit Card payments module in iOS App. Swift 4.0

MFCard Buy me a coffee MFCard is an awesome looking Credit Card input & validation control. Written in Swift 3. YOoo, Now MFCard is on Swift 5. Swift

MobileFirst 362 Nov 29, 2022
A credit card reader and parser for iOS Using Native Vision/VisionKit

card-reader-ios A credit card reader and parser for iOS Using Native Vision/VisionKit May-14-2021.00-43-17.mp4 Instructions Hold camera up to a card a

Khalid Asad 104 Dec 15, 2022
CreditCardForm is iOS framework that allows developers to create the UI which replicates an actual Credit Card.

CreditCardForm CreditCardForm is iOS framework that allows developers to create the UI which replicates an actual Credit Card. Fixed typo use CreditCa

Orazz 1.4k Dec 15, 2022
TextFieldFormatter - Simple Text Formatter (Credit Card Number, Phone Number, Serial Number etc.)

TextFieldFormatter Simple Text Formatter (Credit Card Number, Phone Number, Seri

Anıl ORUÇ 4 Apr 4, 2022
A credit card scanner for iOS written in Swift

DGCardScanner A credit card scanner Requirements iOS 13.0+ Swift 5.5+ Xcode 10.0+ Installation SPM File > Add Packages > https://github.com/donggyushi

donggyu 9 Jun 24, 2022
Ios-card-transition - iOS CocoaPod to create beautiful card transitions

CSCardTransition CSCardTransition is a small library allowing you to create wond

Creastel 12 Oct 31, 2022
Card Decks is a small utility application for your iPhone, iPod touch and iPad which brings you simple, configurable, colored, multi-line text cards that are grouped into card decks

Card Decks is a small utility application for your iPhone, iPod touch and iPad which brings you simple, configurable, colored, multi-line text cards that are grouped into card decks.

Arne Harren 40 Nov 24, 2022
Accept credit cards and PayPal in your iOS app

Important: PayPal Mobile SDKs are Deprecated. The APIs powering them will remain operational long enough for merchants to migrate, but the SDKs themse

PayPal 973 Dec 18, 2022
Ruby Gem for Rails - Easy iTunes In-App Purchase Receipt validation, including auto-renewable subscriptions

Monza is a ruby gem that makes In-App Purchase receipt and Auto-Renewable subscription validation easy. You should always validate receipts on the ser

Gabriel 159 Jan 7, 2023
A Payment Card UI & Validator for iOS

Description Caishen provides an easy-to-use text field to ask users for payment card information and to validate the input. It serves a similar purpos

Prolific Interactive 766 Dec 28, 2022
Bank Card Generator with Swift using SnapKit DSL 💳

iCard BankCard & CreditCard generator with Swift 3 using SnapKit DSL iCard is a simple tool for generate Credit & Bank Card , it represent cards as UI

Emrah Korkmaz 334 Nov 28, 2022
Welcome Busines Card Built With Swift

BusinessCard Welcome Busines Card Main screen Contacts screen More info screen

Konstantin Ryabtsev 0 Dec 28, 2021
A card viewer demo for Idolmaster Millionlive Theater Days written in Swift UI

Misaki Gallery A Millionlive's cards gallery to demostrate Swift UI. All api and

Spike 0 Dec 20, 2021
A study card application built with SwiftUI and Combine

StudyCards Description A study card application. I built this application to get

Matt Manion 2 Sep 26, 2022
Code1System Card Moudle

Code1CardLib Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installation Code1

null 1 Aug 4, 2022
SwiftUI BusinessCard - Created iOS Business card app to practice SwiftUI

SwiftUI_BusinessCard Created iOS Business card app to practice SwiftUI

null 0 Jan 29, 2022