I've been stuck on this for awhile now and I was wondering what is the best way to format a textfield for a credit card type format?
Asked
Active
Viewed 5,096 times
-3
-
this is an opinion based question... no general answer possible... also please provide more code and what you have tryed so far and what isn't working or does not what you're expecting it to do. stackoverflow is no coding service. – David Seek Apr 24 '16 at 11:40
-
1the next time you feel like stuck on a problem, use google, http://stackoverflow.com/questions/12083605/formatting-a-uitextfield-for-credit-card-input-like-xxxx-xxxx-xxxx-xxxx – Med Abida Apr 24 '16 at 11:41
1 Answers
-1
You can use Caishen framework.
It updates the contents of the textfield after every edit using this function :
public func formattedCardNumber(cardNumberString: String) -> String {
let regex: NSRegularExpression
let cardType = cardTypeRegister.cardTypeForNumber(Number(rawValue: cardNumberString))
do {
let groups = cardType.numberGrouping
var pattern = ""
var first = true
for group in groups {
pattern += "(\\d{1,\(group)})"
if !first {
pattern += "?"
}
first = false
}
regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions())
} catch {
fatalError("Error when creating regular expression: \(error)")
}
return NSArray(array: self.splitString(cardNumberString, withRegex: regex)).componentsJoinedByString(self.separator)
}

bzz
- 5,556
- 24
- 26