💳 💰 💻
CCValidator (we're very proud of the unique name
- 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.
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.
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 .
NSString *numberAsString = textField.text; CreditCardType creditCardType = [CCValidator typeCheckingPrefixOnlyWithCreditCardNumber:cardNumber]; //check if type is e.g. CreditCardTypeVisa, CreditCardTypeMasterCard or CreditCardTypeNotRecognized
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.
NSString *numberAsString = textField.text; BOOL isFullCardDataOK = [CCValidator validateWithCreditCardNumber:numberAsString];
let numberAsString = textField.text let isFullCardDataOK = CCValidator.validate(creditCardNumber: numberAsString)
This pod doesn't have any extra dependencies.
CCValidator is available through CocoaPods. To install it, simply add the following line to your Podfile:
Mariusz Wisniewski, Digital Forms
CCValidator is available under the MIT license. See the LICENSE file for more info.