Since I do not know how many special characters exist, it is difficult to verify that the string contains a special character in the white list. Perhaps checking the string containing only the alphabet or numbers is more efficient.
for example kotlin
fun String.hasOnlyAlphabetOrNumber(): Boolean {
val p = Pattern.compile("[^a-zA-Z0-9]")
if (p.matcher(this).matches()) return false
return true
}
for swift4
func hasOnlyAlphabetOrNumber() -> Bool {
if self.isEmpty { return false }
do {
let pattern = "[^a-zA-Z0-9]"
let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
return regex.matches(in: self, options: [], range: NSRange(location: 0, length: self.count)).count == 0
} catch {
return false
}
}
source
share