You have 2 ways :
a- wait till the user has completed the textField and hit return to test the content is correct
b- test after each key press that format is correct.
In anycase, the most effective would be to use a regular expression to test, but if yopu prefer to do it manually, can do this:
For case a, connect the UITextField sentMessages "DidEndOnExit" and "EditingDidEnd" to an IBAction
@IBAction func editingFinished(_ sender: UITextField) {
let currentInput = sender.text!
if currentInput.count > 5 || currentInput.count < 3 { // Refuse more than 5 chars as 23:59 or less than 3 chars but accepts 123 as 1:23
sender.text = "" // Clear it
// Could play a beep
return
}
var posOfColon = currentInput.index(of: ":")
let withColon = posOfColon != nil
var hh = ""
var mm = ""
if withColon {
hh = String(currentInput[..<posofcolon!])
let mmIndex = currentInput.index(posOfColon!, offsetBy: 1)
mm = String(currentInput[mmIndex...])
} else {
mm = String(currentInput.suffix(2)) // Always 2 digits for minutes
if currentInput.count == 4 { // format as 1234
hh = String(currentInput.prefix(2)) // first 2 digits
} else { // Only 3 chars
hh = String(currentInput.prefix(1)) // Only one digit here
}
}
if Int(hh) == nil || Int(hh)! > 23 {
sender.text = "" // Clear it
// Could play a beep
return
}
if Int(mm) == nil || Int(mm)! > 59 {
sender.text = "" // Clear it
// Could play a beep
return
}
// Use hh and mm as needed
}
For case b connect "Editing changed" to an IBAction
@IBAction func editingTestField(_ sender: UITextField) {
}