Skip to content

Instantly share code, notes, and snippets.

@xieweiAlex
Created August 12, 2019 15:53
Show Gist options
  • Save xieweiAlex/92f1ac8fedf3a55679f30c4cdd93f7e5 to your computer and use it in GitHub Desktop.
Save xieweiAlex/92f1ac8fedf3a55679f30c4cdd93f7e5 to your computer and use it in GitHub Desktop.
Check string is alphanumeric
extension String {
var isAlphanumeric: Bool {
return !isEmpty && range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil
}
}
@xieweiAlex
Copy link
Author

"".isAlphanumeric // false
"abc".isAlphanumeric // true
"123".isAlphanumeric // true
"ABC123".isAlphanumeric // true
"iOS 9".isAlphanumeric // false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment