0

I set this format to input for the expiration date credit card for example:
"12/22" format --> mm/yy

https://stackoverflow.com/a/47077265/15324640

func putDateCardFormat(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if range.length > 0 {
  return true
}
if string == "" {
  return false
}
if range.location > 4 {
  return false
}
var originalText = textField.text
let replacementText = string.replacingOccurrences(of: " ", with: "")

if !CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: replacementText)) {
  return false
}
if range.location == 2 {
  originalText?.append("/")
  textField.text = originalText
}

return true
}

is it possible validate year and month in the same function ? I mean validate to avoid this cases:
"45/03"
"56/00"
And if it is possible what is the best / elegant way to do ? Maybe regex ?

LgSus97
  • 107
  • 6

0 Answers0