Checking if a string contains any special characters in swift while allowing all languages

I saw a similar post to this but none of the answers there helped me. I am looking to check if a string does not contain any special characters using swift regular expressions. I want to allow other iPhone keyboard letters though like Chinese, Hindi, Arabic, Korean, etc.. I only do not want to allow special characters like +, &, #, $, % though.

I have tried using:

func usernameTest(testStr:String) -> Bool {

        return testStr.range(of: "^[ !\"#$%&'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+", options: .regularExpression) != nil
    }

This did not work though. I thought it would check to see if any special characters were used, but when I tested phrases like "Tom@@@." and "%Will!!" it returned false. I would have expected this to return true since the strings that I passed in contained one or more of the special characters in the range.

Any help would be appreciated as we want our users to be able to create their usernames in any language but we still do not want to allow spaces or special characters.


you can try this way

var charSet = CharacterSet.init(charactersIn: "@#$%+_)(")
    var string2 = "test@3"

    if let strvalue = string2.rangeOfCharacter(from: charSet)
    {
        print("true")
    }

in the characterset init you can give the special characters which you want to check the occurance.

Check if string contains special characters in Swift, What you want is to check if any character is not in the given set: set will work, because in your character set you have all the valid characters: A-Z and a-z when using the caseInsensitive option, as Swift covers that I prefer to detect the invalid characters, rather than provide an allowed character set. Check if string contains special characters in Swift Swift iOS Mobile Development iPhone To check if a string contains a special character in swift we can use conditionals like if else or switch but that would need a lot of conditions to be executed, making programming as well as execution time consuming.


You must remove character "^" in the regex. So your function should look like this:

func usernameTest(testStr:String) -> Bool {
return testStr.range(of: "[ !\"#$%&'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+", options: .regularExpression) != nil }

Check if string contains special characters in Swift, To check if a string contains a special character in swift we can use conditionals like if else or switch but that would need a lot of conditions to� Home » Swift » Check if string contains special characters in Swift Check if string contains special characters in Swift Posted by: admin November 29, 2017 Leave a comment


func usernameTest(testStr:String) -> Bool {
     let letters = CharacterSet.punctuationCharacters
     let range = testStr.rangeOfCharacter(from: letters)
     // range will be nil if no letters is found
     if range != nil {
        return true
     }
     else {
        return false
     }
}

try this one

Check if string contains special characters in Swift, Swift string contains characterset alphanumerics let string = "my-string_" if (string. From Swift 1.2 you can do that using Set. var str = "Hello, World!" let charset: Set<Character> = ["e", "n"] charset. isSubsetOf(str) // `true` if `str` contains all characters in `charset` charset. Strings and Characters; Check if String contains Characters from a Defined Set; Concatenate strings; Convert String to and from Data / NSData; Converting Swift string to a number type; Count occurrences of a Character into a String; Examine and compare strings; Formatting Strings; Remove characters from a string not defined in Set


Check if string contains special characters in Swift, remove all special characters from string swift 4 swift check if string I have to detect whether a string contains any special characters. How can I check it? Check string only contains numbers. shell script to check whether a character is vowel or consonant. shell script to check if string contains vowels. bash check if string starts with character. bash check if string ends with character. shell script to check whether a character is alphabet digit or special character. bash check if string


Strings and Characters — The Swift Programming Language (Swift , Every string is composed of encoding-independent Unicode characters, and provides When your source code includes a line break inside of a multiline string literal, that line If you need the special effects of a character in a string literal, match the It enables you to represent almost any character from any language in a� Python program to check if a string contains any unique character; Check if string contains special characters in Swift; Java Program to check if the String contains any character in the given set of characters; C# Program to replace a special character from a String; Check if a string contains a sub-string in C++; C# Program to check if a


Strings and Characters, Because Swift Strings are based on Unicode, they may be any Unicode scalar value, including Check if String contains Characters from a Defined Set. Letters . Given a string, the task is to check if that string contains any special character (defined special character set). If any special character found, don’t accept that string.