-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.

3
  • Does this answer your question? swift - validating UITextField Commented May 27, 2020 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 } Commented May 29, 2020 at 0: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" } Commented May 29, 2020 at 0:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.