1

I am having trouble right now. I set a custom TextField. When users type any word in the box, I want the data to be saved, but I want to set a condition which is every time a word is entered, only one word is allowed in a String text. For example, if a user enters a text which is like "Hello", es, I want it to be saved because as it fulfills the condition. However, if it is "hello, world", "hello ", " hello", "I am a super man" or anything like a text with some space in a String text, it is not allowed.

for space in text {
     if space == " " {
         print("Enter only one word please")
         return
     }
}

This is my attempt to solve this, but it is not working well. I wonder if I should use an array to achive my goal but I have no idea. User inputs should be allowed if there is no space.

Thank you.

Ryohei
  • 713
  • 2
  • 9
  • 20
  • 2
    You should use swift String class `contains(otherString: Sting)` method. like `text.contains(" ")`. It will return true if string contains space – Aks Oct 18 '16 at 15:28
  • @Aks Hey, awesome! You solved my isssu!!! It was so simple aproach to do this! Thanks man! – Ryohei Oct 18 '16 at 15:39

4 Answers4

3

Use this function to check if there are any spaces in your string:

func validate(string: String) -> Bool {
    return string.rangeOfCharacter(from: CharacterSet.whitespaces) == nil
}

Or this one to check if input contains only letters:

func validate(string: String) -> Bool {
    return string.rangeOfCharacter(from: CharacterSet.letters.inverted) == nil
}

An example of how to actually use it:

class ViewController: UIViewController, UITextFieldDelegate {
    //...
    func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
        let currentString: NSString = (textField.text ?? "") as NSString
        let newString = currentString.replacingCharacters(in: range, with: string)
        return  validate(string: newString)
    }
}
alexburtnik
  • 7,661
  • 4
  • 32
  • 70
1

If you only want to check for spaces, your code is almost correct. You only need to change this:

for space in text {

to this:

for space in text.characters {

If you want to check for all whitespaces, try this:

for c in text.characters {
    let string = String(c)
    let unicodeScalar = s.unicodeScalars.first!
    if CharacterSet.whitespacesAndNewlines.contains(unicodeScalar) {
        // contains whitespace
    }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • You can avoid looping through the characters by using `rangeOfCharacter` method. It is shorter and more readable as for me. – alexburtnik Oct 18 '16 at 15:39
  • This approach is interesting to me! Is there any difference between just writing text.contains(" ")? – Ryohei Oct 18 '16 at 15:53
  • @ryohei that only checks for spaces. If you want to check for all white spaces you can't use that. – Sweeper Oct 18 '16 at 16:00
0

I think this will solve your issue.

let phrase = “Your text here“

let whitespace = NSCharacterSet.whitespaceCharacterSet()
let range = phrase.rangeOfCharacterFromSet(whitespace)

// range will be nil if no whitespace is found
if let test = range {
    print("Enter only one word please")
         return
}

Inspired from the link here

Community
  • 1
  • 1
Praveen Kumar
  • 1,338
  • 3
  • 20
  • 31
-1

Use regular expression to solve this validation.

let stringInput = "epninijjk werewr"

let regEx = "(^[a-zA-Z0-9]{2,30}$)"

do {
let reularExpression = try NSRegularExpression(pattern: regEx, options: .CaseInsensitive)
let reuslt = reularExpression.matchesInString(stringInput, options: [], range: NSRange(location: 0, length: email.characters.count))
if reuslt.isEmpty{
    print("Not valid")
}
else{
    print("Valid")
} 
}
catch let error {
    print(error)
}

Here {2,30} 2 is the minimum number of characters and 30 is the maximum limit.

You can change according to your requirement.

Add special characters in [a-zA-Z0-9] , if you want to allow.

user3608500
  • 835
  • 4
  • 10
  • I found @Aks answer was fairly simple and easy to solve my issue. This looks so complicated for now i guess, but thanks for helping me figure out this issue! – Ryohei Oct 18 '16 at 15:40