Skip to content

Instantly share code, notes, and snippets.

@luki
Created April 5, 2016 14:44
Show Gist options
  • Save luki/f61537d0e64ec04a1e6509f97dcde68f to your computer and use it in GitHub Desktop.
Save luki/f61537d0e64ec04a1e6509f97dcde68f to your computer and use it in GitHub Desktop.
Checks if typed in message contains a character that doesn't fit A-Z, 0-9 & _.
import Foundation
import UIKit
func unallowedCharacter(word: String, alert: Bool) -> AnyObject {
var arrayOfString = Array(word.characters)
var containsUnallowedCharacter = false
for i in 0...arrayOfString.count - 1 {
var currentCharacter: String = String(arrayOfString[i]).lowercaseString
if currentCharacter != "a"
&& currentCharacter != "b"
&& currentCharacter != "c"
&& currentCharacter != "d"
&& currentCharacter != "e"
&& currentCharacter != "f"
&& currentCharacter != "g"
&& currentCharacter != "h"
&& currentCharacter != "i"
&& currentCharacter != "j"
&& currentCharacter != "k"
&& currentCharacter != "l"
&& currentCharacter != "m"
&& currentCharacter != "n"
&& currentCharacter != "o"
&& currentCharacter != "p"
&& currentCharacter != "q"
&& currentCharacter != "r"
&& currentCharacter != "s"
&& currentCharacter != "t"
&& currentCharacter != "u"
&& currentCharacter != "v"
&& currentCharacter != "w"
&& currentCharacter != "x"
&& currentCharacter != "y"
&& currentCharacter != "z"
&& currentCharacter != "0"
&& currentCharacter != "1"
&& currentCharacter != "2"
&& currentCharacter != "3"
&& currentCharacter != "4"
&& currentCharacter != "5"
&& currentCharacter != "6"
&& currentCharacter != "7"
&& currentCharacter != "8"
&& currentCharacter != "9"
&& currentCharacter != "_" {
containsUnallowedCharacter = true
} else {
containsUnallowedCharacter = false
}
}
if containsUnallowedCharacter == true {
if alert == false {
return containsUnallowedCharacter
} else {
let characterAlert = UIAlertController(title: "Your username contains at least one invalid character. You are allowed to type in A-Z, 0-9 and _", message: nil, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) {
(action) in
print("Okay")
}
characterAlert.addAction(action)
return characterAlert
}
} else {
return containsUnallowedCharacter
}
}
unallowedCharacter("Hezü", alert: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment