UITextView User Input Text Validation in Swift 4 or Swift 5: Restrict the specific characters in Swift

483 Views Asked by At

Need help on how to do a text input validation to a UITextView. Many help is there for UITextField, but I can't find the right solution for this textView input validation. My expectation is to a UITextView needs to accept only these characters[A-Za-z0-9.-] what to do any help or solution. My textView is connected to the ibOutlet, Made it as self, even added the subclass as UItextViewDelegate still I can't find solution.

1

There are 1 best solutions below

0
Danni André On

You can check if there is at least one character that not conforms to the alphanumeric characterSet.

The method:

yourTextView.text.rangeOfCharacter(from: CharacterSet.alphanumerics.inverted)

should return nil if there is no alphanumeric character.