vitkuzmenko / creditcardvalidator Goto Github PK
View Code? Open in Web Editor NEWCredit Card Validator in Swift
License: MIT License
Credit Card Validator in Swift
License: MIT License
Hello,
let digit = Int(String(s))! in validate() of CreditCardValidator is crashing.
Dependency "CreditCardValidator" has no shared framework schemes for any of the platforms: iOS
If you believe this to be an error, please file an issue with the maintainers at https://github.com/vitkuzmenko/CreditCardValidator/issues/new
The new credit card for MasterCard BIN 2221 - 2720
Hi,
Library is very useful and would be great to have it in SPI by Dave and Sven.
Guide to add: https://swiftpackageindex.com/add-a-package
Thanks
The app example app says that this is a valid Visa card number: 4242424242
All of the sources I can find say a Visa card must have 13, 16, or 19 characters. That number has 10 digits. Are you certain this is correct?
Obviously the auto-converter tool is not perfect and lets some loose ends. I have found this working solution for CreditCardValidator.swift
`
import Foundation
open class CreditCardValidator {
open lazy var types: [CreditCardValidationType] = {
var types = [CreditCardValidationType]()
for object in CreditCardValidator.types {
types.append(CreditCardValidationType(dict: object as [String : AnyObject]))
}
return types
}()
public init() { }
/**
Get card type from string
- parameter string: card number string
- returns: CreditCardValidationType structure
*/
open func typeFromString(_ string: String) -> CreditCardValidationType? {
for type in types {
let predicate = NSPredicate(format: "SELF MATCHES %@", type.regex)
let numbersString = self.onlyNumbersFromString(string)
if predicate.evaluate(with: numbersString) {
return type
}
}
return nil
}
/**
Validate card number
- parameter string: card number string
- returns: true or false
*/
open func validateString(_ string: String) -> Bool {
let numbers = self.onlyNumbersFromString(string)
if numbers.characters.count < 9 {
return false
}
var reversedString = ""
let range = Range<String.Index>(uncheckedBounds: (lower: numbers.startIndex, upper: numbers.endIndex))
numbers.enumerateSubstrings(in: range, options: [NSString.EnumerationOptions.reverse, NSString.EnumerationOptions.byComposedCharacterSequences]) { (substring, substringRange, enclosingRange, stop) -> () in
reversedString += substring!
}
var oddSum = 0, evenSum = 0
let reversedArray = reversedString.characters
var i = 0
for s in reversedArray {
let digit = Int(String(s))!
if (i+1) % 2 == 0 {
evenSum += digit
} else {
oddSum += digit / 5 + (2 * digit) % 10
}
}
return (oddSum + evenSum) % 10 == 0
}
/**
Validate card number string for type
- parameter string: card number string
- parameter type: CreditCardValidationType structure
- returns: true or false
*/
open func validateString(_ string: String, forType type: CreditCardValidationType) -> Bool {
return typeFromString(string) == type
}
open func onlyNumbersFromString(_ string: String) -> String {
let set = CharacterSet.decimalDigits.inverted
let numbers = string.components(separatedBy: set)
return numbers.joined(separator: "")
}
// MARK: - Loading data
fileprivate static let types = [
[
"name": "Amex",
"regex": "^3[47][0-9]{5,}$"
], [
"name": "Visa",
"regex": "^4[0-9]{6,}$"
], [
"name": "MasterCard",
"regex": "^5[1-5][0-9]{5,}$"
], [
"name": "Maestro",
"regex": "^(?:5[0678]\\d\\d|6304|6390|67\\d\\d)\\d{8,15}$"
], [
"name": "Diners Club",
"regex": "^3(?:0[0-5]|[68][0-9])[0-9]{4,}$"
], [
"name": "JCB",
"regex": "^(?:2131|1800|35[0-9]{3})[0-9]{3,}$"
], [
"name": "Discover",
"regex": "^6(?:011|5[0-9]{2})[0-9]{3,}$"
], [
"name": "UnionPay",
"regex": "^62[0-5]\\d{13,16}$"
]
]
}
`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.