-1

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.

  • Does this answer your question? [swift - validating UITextField](https://stackoverflow.com/questions/31495188/swift-validating-uitextfield) – Chris May 27 '20 at 14:52
  • ANSWER(Bcoz i can't answer to my question) PART 1 #1.Created a Public String Property var isValidComment: Bool { //Special Characters Input Validation let characterset = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .-") return self.rangeOfCharacter(from: characterset.inverted) != nil } – Balaji Anandan May 29 '20 at 00:25
  • ANSWER Part 2 #2. Then i validated the UITextView/textView field with this string validation property with an error text. That's it. if !eventComments.text.isValidComment { errorLabel.text = "Please enter Event Comments between 4 - 60 Characters" } else { errorLabel.text = "Allowed Event Comments Characters are a-z A-Z 0-9" } – Balaji Anandan May 29 '20 at 00:25

1 Answers1

0

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.