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?