1

I am trying to prevent users from entering the characters '#' and empty spaces (' '). I have got as far as below (see code below) but not sure how to complete the rest ...

 class CreateTags: UIViewController, UITextFieldDelegate {

 /*** OUTLETS ***/
 @IBOutlet weak var inputTxtOutlet: UITextField!


 override func viewDidLoad() {
      self.inputTxtOutlet.delegate = self
 }

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    return true
  }

 }
Dimple
  • 788
  • 1
  • 11
  • 27
dean
  • 321
  • 3
  • 17

7 Answers7

2

You need o implement following delegate method (as you already implemented)

Set delegate

yourtextField.delegate = self

Delegate method

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if string == "#" || string == " " {
        return false //disallow # and a space
    }
    return true

 }

In another way you can do it as follow

Create a constant that contains disallowed chars.

 let disallowedChars = ["#", " "] //add more chars as per your need

In delegate method check those chars...

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

      if disallowedChars.contains(string) {
        return false
      }

      return true
}
Mahendra
  • 8,448
  • 3
  • 33
  • 56
2

Here is a one-liner. (Works for copy paste!)

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    return !string.contains(where: {$0 == "#" || $0 == " "})
}

If you have a lot of restricted characters,

let restrictedCharacters: [Character] = [" ", "#", "?"]

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
   return !(Set(string).intersection(Set(restrictedCharacters)).count > 0)
}
Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
2

Try to be specific for UITextfields!

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

 if textField == inputTxtOutlet  {
       let myCharSet = CharacterSet(charactersIn:" #")
       let output: String = string.trimmingCharacters(in: myCharSet.inverted)
       let isValid: Bool = (string == output)

        if isValid {
             return false
        } else {
             return true
                }
            }
    return true
}
Dimple
  • 788
  • 1
  • 11
  • 27
2
let restrictedChars = ["#", "*", " "]

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
   return  restrictedChars.contains(string) ? false : true 
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

Here is your solution:-

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    let characters = ["#", "$", "!", "&","@"]
    for character in characters{
        if string == character{
            print("This characters are not allowed")
            return false
        }
    }
}

uses this How to restrict certain characters in UITextField in Swift? example

Wings
  • 2,398
  • 23
  • 46
0
extension String {

   var containsValidCharacter: Bool {
   guard self != "" else { return true }
   let hexSet = CharacterSet(charactersIn: "1234567890ABCDEFabcdef")
   let newSet = CharacterSet(charactersIn: self)
   return hexSet.isSuperset(of: newSet)

 }
}

You use it like with the UITextFieldDelegate.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return (string.containsValidCharacter)
}
0

Replace Your "shouldChangeCharactersIn" method with below code

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let characters = ["#", " "]
    for character in characters{
        if string == character{
            print("This characters are not allowed")
            return false
        }
    }
    return true
}