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?

It's now been three months since I started this thread. I there nobody who has tried to solve this?

1 Like

I think there are a couple of articles on this topic. Have you tried a google search? One way would be to have your own @Binding with a custom setter that does the validation on the newValue.

I still haven't found a decent solution. Have you by chance?

See here and here

1 Like

Thanks! I'll give these a look.