I have a textfield which allows the user to input text and saves this text to a variable and stores it in user defaults. I need the input to be a string therefore I need the textfield to detect the input as being anything but a string. How can I do this?
Asked
Active
Viewed 3,968 times
-1
-
1What do you not consider a string in a textField? – alexburtnik Nov 16 '16 at 18:35
-
Everything in that text field is a string. You'll need to determine your own rules about what you consider a valid string and compare against those. – Chris Slowik Nov 16 '16 at 18:39
-
I've rephrased the question name to clarify; I need to essentially validate the input to check it is a string (cannot be integers/doubles such as 5 or 5.3) – Antonio C Nov 16 '16 at 18:42
-
You need only alphabetical characters in your textfield ? – Rajat Nov 16 '16 at 18:46
-
yes it needs to be alphabetical @Rajat as the textfield is used to enter a name. BTW the textfielf is in a alert controller – Antonio C Nov 16 '16 at 18:52
4 Answers
0
You can use this code
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let characterSet = NSCharacterSet.letters
return (string.rangeOfCharacter(from: characterSet) != nil)
}
make sure you have set delegate
for your textfield
properly

Rajat
- 10,977
- 3
- 38
- 55
-
I know this solution gets used over and over but this is really a terribly inefficient way to verify the characters in the string. – rmaddy Nov 16 '16 at 19:10
-
-
It's very inefficient to split the entire string into an array and then rejoin all of the array elements back into a string and then compare the two strings just to see if the original string contains valid characters. – rmaddy Nov 16 '16 at 19:17
0
Try this code. Hope it helps you.
func updateSaveButtonState() {
let text = titleTextField.text ?? ""
saveButton.isEnabled = !text.isEmpty
}
@IBAction func textEditingChanged(_ sender: UITextField) {
updateSaveButtonState()
}
@IBAction func returnPressed(_ sender: UITextField) {
titleTextField.resignFirstResponder()
}

Vitalik Kizlov
- 385
- 5
- 9
0
try with below code: swift 4.0
func validateOnlyLetters(text: String) -> Bool {
let characterSet = CharacterSet.decimalDigits
return (text.rangeOfCharacter(from: characterSet) == nil)
}

Van
- 1,225
- 10
- 18
-1
by default, textField.text property is a string. if you don't want the users to enter any numbers, you may refer to this post to vaidate the input before saving into userdefaults.
another post in Swift - How can I check if a string contains letters in Swift?
let letters = NSCharacterSet.letterCharacterSet()
let phrase = "Test case"
let range = phrase.rangeOfCharacterFromSet(letters)
// range will be nil if no letters is found
if let test = range {
println("letters found")
}
else {
println("letters not found")
}
-
Yes that's exaclty what I need; to validate the input before saving. The post is in Objective C however I am using swift and don't understand it – Antonio C Nov 16 '16 at 18:40
-
http://stackoverflow.com/questions/29520618/how-can-i-check-if-a-string-contains-letters-in-swift – jokerday Nov 17 '16 at 13:30
-