Trying to have a textField
for Credit Card expiry date the below code is working properly just looking to change the behavior, currently when you are typing from the third number / character will be added as an expiration format. What if I want to add the / character once user type the second number for example if the user typed 01 directly insert the separator character
open func reformatAsExpiration(_ textField: UITextField) {
guard let string = textField.text else { return }
let expirationString = String(ccrow.expirationSeparator)
let cleanString = string.replacingOccurrences(of: expirationString, with: "", options: .literal, range: nil)
if cleanString.length >= 3 {
let monthString = cleanString[Range(0...1)]
var yearString: String
if cleanString.length == 3 {
yearString = cleanString[2]
} else {
yearString = cleanString[Range(2...3)]
}
textField.text = monthString + expirationString + yearString
} else {
textField.text = cleanString
}
}