Tarek
Tarek

Reputation: 823

No consecutive spaces Swift UITextField

How do I disable the user from typing two consecutive spaces in a UITextField?

The code I tried using is as follows:

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

    let myString = myUITextField.text

    let regex = try? NSRegularExpression(pattern: " +", options: .caseInsensitive)
    let trimmedString: String? = regex?.stringByReplacingMatches(in: myString!, options: [], range: NSRange(location: 0, length: (myString?.characters.count)!), withTemplate: " ")

    myUITextField.text = trimmedString

    let maxLegnth = 40
    let currentString: NSString = textField.text! as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

    return newString.length <= maxLegnth
}

PS: The rest of the code forces the user to type in 40 characters only. This is another requirement.

Upvotes: 2

Views: 3253

Answers (7)

Saurabh Sharma
Saurabh Sharma

Reputation: 1968

 //User never input space in text field(swift 5)...

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
     if string == " "   {
       // If consecutive spaces entered by user
       return false
     }
 }

Upvotes: 0

Tarek
Tarek

Reputation: 823

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let maxLegnth = 40
    let currentString: NSString = textField.text! as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

    if textField.text?.characters.last == " " && string == " " {
        return false
    } else {
        return newString.length <= maxLegnth
    }
}

Upvotes: 1

Jobelle
Jobelle

Reputation: 2834

Try this regular expression

var pattern=/(\s+){2,}/

 if(pattern.test(stringToCheck))
     {
    // string contain more than one consecutive space 
     }

Upvotes: -1

Dhruv
Dhruv

Reputation: 72

Swift 3,Xcode 8.2

Prevent user from typing spacebar keys

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

    //Restrict
    if string == " " && textField.text?.characters.last == " "{
        return false
    }else{
        return true
    }
}

Upvotes: 1

Maddy
Maddy

Reputation: 1658

Just check the previous and the current character entered by the user.

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

if textField.text?.characters.last == " "  && string == " "{

// If consecutive spaces entered by user               
 return false
}

// If no consecutive space entered by user

return true

}

Upvotes: 4

Tony
Tony

Reputation: 59

how about replace " "(two spaces) with " "(one spaces) all the time.

Upvotes: -1

Rohan Bhale
Rohan Bhale

Reputation: 1345

You can combine the current displayed text in the textfield, with the new text that is being added. Then you can check for the occurence of " ". If this string is detected return false

Upvotes: 2

Related Questions