CCValidator 1.0.2

CCValidator 1.0.2

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Mar 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Mariusz Wisniewski.


Downloads

Total11,390
Week0
Month0

Installs

Apps80
Test Targets4
powered by Segment

GitHub

Stars6
Watchers0
Forks3
Issues1
Contributors2
Pull Requests0

Code

Files3
LOCLines of Code 265


  • By
  • Mariusz Wisniewski

CCValidator 💳💰💻

BuddyBuild

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