Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Credit card validation in Swift 3
import UIKit
import PlaygroundSupport
//http://stackoverflow.com/questions/72768/how-do-you-detect-credit-card-type-based-on-number
enum CreditCardType {
case visa
case visaElectron
case mastercard
case maestro
case americanExpress
case dinnersClub
case discovery
case jcb
static var all: [CreditCardType] {
return [
.visa,
.visaElectron,
.mastercard,
.maestro,
.americanExpress,
.dinnersClub,
.discovery,
.jcb
]
}
var pattern: String {
switch self {
case .visa: return "^4[0-9]{12}(?:[0-9]{3})?$"
case .visaElectron: return "^(4026|417500|4508|4844|491(3|7))"
case .mastercard: return "^5[1-5][0-9]{14}$"
case .maestro: return "^(5018|5020|5038|6304|6759|676[1-3])"
case .americanExpress: return "^3[47][0-9]{13}$"
case .dinnersClub: return "^3(?:0[0-5]|[68][0-9])[0-9]{11}$"
case .discovery: return "^6(?:011|5[0-9]{2})[0-9]{12}$"
case .jcb: return "^(?:2131|1800|35\\d{3})\\d{11}$"
}
}
var exampleValue: String {
switch self {
case .visa: return "4111111111111111"
case .visaElectron: return "4026000000000002"
case .mastercard: return "5538383883833838"
case .maestro: return "501800000009"
case .americanExpress: return "347000000000000"
case .dinnersClub: return "30099999999999"
case .discovery: return "6550000000000000"
case .jcb: return "180000000000000"
}
}
}
struct Validator {
let cardType: CreditCardType
let value: String
func test() -> Bool {
do {
let regex = try NSRegularExpression(pattern: cardType.pattern, options: .caseInsensitive)
return regex.matches(in: value, options: [], range: NSMakeRange(0, value.count)).isEmpty == false
} catch {
return false
}
}
}
let v = Validator(cardType: .americanExpress, value: CreditCardType.americanExpress.exampleValue)
v.test()
@sungkim23
Copy link

sungkim23 commented Feb 20, 2017

Hey! Found your code on Google, how can we go about using this?

Say with:
xxxTextField.text.characters.first== "3" { }

@karthikmandava7
Copy link

karthikmandava7 commented Nov 1, 2018

Hi Nuno Gonçalves, is there any demo App for this ? I'm unable to understand how to use it.

@Hrayr2112
Copy link

Hrayr2112 commented Apr 3, 2019

Nice!

@cci-emciftci
Copy link

cci-emciftci commented Mar 22, 2020

let validator = Validator(cardType: .americanExpress, value: xxxTextField.text) //`xxxTextField.text` does this text match the given type (.americanExpress). If it is correct text for given type it returns true.
print(validator.test()) 

@nunogoncalves
Copy link
Author

nunogoncalves commented Mar 22, 2020

Wow! I only noticed the comments now thanks to @emrcftci! Sorry about that.
I realise this is very old and probably won't help anyone but this is being used here: https://github.com/nunogoncalves/iOS-CreditCardForm
More specifically here:
https://github.com/nunogoncalves/iOS-CreditCardForm/blob/master/CreditCard/CreditCardTypeChecker.swift

@cci-emciftci
Copy link

cci-emciftci commented Mar 26, 2020

I'll use this Validator and I'll make some improvements. I'll inform you when I finish my library. Thanks for the gist @nunogoncalves 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment