How to validate keystrokes in SwiftUI?

The question is in the subject.

Maybe you can use onEditingChange to monitor the changes to the text and do your validation.

You could even add a didSet to the variable that holds the text and do your validation there perhaps? Something like this.

Will that prevent invalid characters appearing in the text field?