How can I write two characters through "/" in a text box? We need this format "MM / YY", the user will enter just numbers and they are automatically set like this 01/01
override func viewDidLoad() {
super.viewDidLoad()
self.expiresTextField.addTarget(self, action: #selector(didChangeText(textField:)), for: .editingChanged)
}
@objc func didChangeText(textField:UITextField) {
expiresTextField.text = self.expiresString(creditCardExpiresString: textField.text!)
}
func expiresString(creditCardExpiresString : String) -> String {
//here you need to write an implementation
return creditCardExpiresString
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newLength = (textField.text ?? "").count + string.count - range.length
if(textField == expiresTextField) {
return newLength <= 5
}
return true
}