Skip to content
πŸ’³ Credit Card Validator. Check users' credit card input and their card type easily.
Swift Ruby Objective-C
Branch: master
Clone or download
Latest commit 60eafa5 Jul 27, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CCValidator.xcodeproj Swift 4.2 migration Dec 5, 2018
CCValidator.xcworkspace
CCValidator Swift 4.2 migration Dec 5, 2018
Tests Version 1.0.0 Feb 21, 2017
.gitignore Version 1.0.0 Feb 21, 2017
.swift-version Version 1.0.0 Feb 21, 2017
CCValidator.podspec Version 1.0.2 Mar 24, 2017
LICENSE Version 1.0.0 Feb 21, 2017
README.md Version 1.0.2 Mar 24, 2017

README.md

CCValidator πŸ’³πŸ’°πŸ’»

BuddyBuild Version License Platform

CCValidator (we're very proud of the unique name πŸ˜‰) provides validation for credit card number input.

  • Validate card numbers using Luhn algorithm,
  • Detect credit card type before user provides full card number (this way, you can tell your user you don't support card XXX early, without frustrating him only after he typed all 16 digits).

Opposing to other existing validation libraries, this one doesn't use Regex. It was inspired by CreditCardJS and its purpose it to provide a clear code, understanding which doesn't require knowledge of regex -- everyone should be able to read and edit validation code with ease.

Plus, ability to detect possible card type even from only first digit, lets you fail early.

Example

This pod doesn't contain an example app, but tests should show you how to use it properly.

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

Usage

Get type from prefix

To get type from card prefix, pass credit card input you have (can be as short as 1 character string). Validator will check it against all types it knows and return recognized type, or .NotRecognized.

Obj-C:

NSString *numberAsString = textField.text;
CreditCardType creditCardType = [CCValidator typeCheckingPrefixOnlyWithCreditCardNumber:cardNumber];
//check if type is e.g. CreditCardTypeVisa, CreditCardTypeMasterCard or CreditCardTypeNotRecognized

Swift:

let numberAsString = textField.text
let recognizedType = CCValidator.typeCheckingPrefixOnly(creditCardNumber: numberAsString)
//check if type is e.g. .Visa, .MasterCard or .NotRecognized

Validate length, type and correctness

You can also validate length and type -- e.g. if validator recognizes card as MasterCard, it won't validate cards with different length than 16 digits. For Visa, it will allow 13, 16 and 19 digits etc.

Next to that, it also validates card number using Luhn algorithm -- this way you can pass to your payment processor only cards that numbers look like correct ones.

Obj-C:

NSString *numberAsString = textField.text;
BOOL isFullCardDataOK = [CCValidator validateWithCreditCardNumber:numberAsString];

Swift:

let numberAsString = textField.text
let isFullCardDataOK = CCValidator.validate(creditCardNumber: numberAsString)

Requirements

This pod doesn't have any extra dependencies.

Installation

CCValidator is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "CCValidator"

Author

Mariusz Wisniewski, Digital Forms

License

CCValidator is available under the MIT license. See the LICENSE file for more info.

Version history

  • 1.0.2 - 2017/03/24
  • 1.0.1 - 2017/02/22
    • Updated documentation
  • 1.0.0 - 2017/02/21
    • Initial version of the library
You can’t perform that action at this time.