2

I am trying to prevent a UITextField from being entered space character or no character, but works only for no character. What I need is: the user is not allowed to save textfield without inputing any character and also just spaces; But the user can input, for example: "New Category". So spaces are only allowed between letters or number, but not only spaces.

Here´s my code:

@IBAction func btnAddCategory(sender: AnyObject) {
    let alert = UIAlertController(title: "Ajouter une catégorie", message: nil, preferredStyle: .Alert)
    alert.view.tintColor = Utils.colorFromHex(0x585858)
    let confirmAction = UIAlertAction(title: "OK", style: .Default, handler: ({ (_) in
        if let field = alert.textFields?[0] {
            if field.text! == NSCharacterSet.whitespaceCharacterSet() || field.text! == ""{
                self.displayAlert("Attention", alertMsg: "Vous ne pouvez pas créer des données vides")
            } else {
                if self.checkDuplicates(field.text!) {
                    self.displayAlert("Attetion", alertMsg: "Vous avez déjà une catégorie avec ce nom !")
                } else {
                    self.saveCategory(field.text!)
                    self.tableView.reloadData()
                }
            }
        }
        }
    ))

    let cancelAction = UIAlertAction(title: "Annuler", style: .Cancel, handler: nil)

    alert.addTextFieldWithConfigurationHandler({(textField) in
        textField.placeholder = "Titre"
        textField.font = UIFont(name: "Roboto-Light", size: 15)!
    })

    alert.addAction(confirmAction)
    alert.addAction(cancelAction)

    self.presentViewController(alert, animated: true, completion: nil)
}

So anyone could help me on this?

4 Answers 4

3

If you want to prevent white spaces while digiting characters, you can implement the textfield(textfield:UITextField, shouldChangeCharactersInRange: NSRange, replacementString: String) -> Bool delegate method of UITexfieldDelegate protocol.
In the implementation you should return false if the the new character is a space or true for something else.
What we are doing here is creating a set of characters to check against.
NSCharacterSet provides already different sets.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        let inValidCharacterSet = NSCharacterSet.whitespaceCharacterSet()
        guard let firstChar = string.unicodeScalars.first else {return true}
        return !inValidCharacterSet.isCharInSet(Character(firstChar))
    }

Where isCharInSet is an extension of NSCharacterSet (I've taken and modified that method from somewhere in S.O.):

extension NSCharacterSet {
    func isCharInSet(char: Character) -> Bool {
        var found = true
        for ch in String(char).utf16 {
            if !characterIsMember(ch) { found = false }
        }
        return found
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

What I need is: the user is not allowed to save textfield without inputing any character and also just spaces; But the user can input, for example: "New Category". So spaces are only allowed between letters or number, but not only spaces.
@Marco Almeida, you should have said that in your original question.
@paulvs My fault, sorry. I edited the question. But do you have any suggestion?
You could use the hasPrefix and hasSuffix methods of String to check whether the UITextField's text string begins or ends with a string.
Whoops, misread your question, wouldn't this do: textField.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).characters.count == 0
|
2

Swift 5.1, Xcode 11

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard range.location == 0 else {
        return true
    }

    let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string) as NSString
    return newString.rangeOfCharacter(from: CharacterSet.whitespacesAndNewlines).location != 0
}

4 Comments

No guidance on how to use this. Not very helpful.
Yes but no explanation on what it is and how it works.
Also if you type any character, you now can't backspace it.
This code only stops the first character from being a blank space, once you start to type you can do anything you want including endless white space
1

Finally managed to get it fixed with this code:

@IBAction func btnAddCategory(sender: AnyObject) {
    let alert = UIAlertController(title: "Ajouter une catégorie", message: nil, preferredStyle: .Alert)
    alert.view.tintColor = Utils.colorFromHex(0x585858)
    let confirmAction = UIAlertAction(title: "OK", style: .Default, handler: ({ (_) in
        if let field = alert.textFields?[0] {

            let strLength = field.text!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).characters.count

            if  strLength == 0 {
                self.displayAlert("Attention", alertMsg: "Vous ne pouvez pas créer des données vides")
            } else {
                if self.checkDuplicates(field.text!) {
                    self.displayAlert("Attetion", alertMsg: "Vous avez déjà une catégorie avec ce nom !")
                } else {
                    self.saveCategory(field.text!)
                    self.tableView.reloadData()
                }
            }
        }
        }
    ))

    let cancelAction = UIAlertAction(title: "Annuler", style: .Cancel, handler: nil)

    alert.addTextFieldWithConfigurationHandler({(textField) in
        textField.placeholder = "Titre"
        textField.font = UIFont(name: "Roboto-Light", size: 15)!
    })

    alert.addAction(confirmAction)
    alert.addAction(cancelAction)

    self.presentViewController(alert, animated: true, completion: nil)
}

Comments

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

        let   trimmedString = string.trimmingCharacters(in: .whitespacesAndNewlines)
        var subString = textField.text as NSString?
        subString = subString?.replacingCharacters(in: range, with: string) as NSString?

        if (textField.tag == 0 || textField.tag == 1)
        {
            let acceptedInput = NSCharacterSet.alphanumerics.inverted
            let filteredString = (trimmedString.components(separatedBy: acceptedInput)).joined(separator: "")

            if trimmedString == filteredString
            {
                textField.text = (textField.text! as NSString).replacingCharacters(in: range, with: filteredString)

                return false
            }
            else
            {
                return false
            }
        }
        return true

    }

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.