Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension String {
- var isAlphanumeric: Bool {
- return !isEmpty && range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil
- }
- }
- "".isAlphanumeric // false
- "abc123".isAlphanumeric // true
- "iOS 9".isAlphanumeric // false
- if yourString.rangeOfCharacter(from: CharacterSet.alphanumerics.inverted) != nil {
- return "Username can only contain numbers or digits"
- }
- extension String {
- func isAlphanumeric() -> Bool {
- return self.rangeOfCharacter(from: CharacterSet.alphanumerics.inverted) == nil
- }
- }
- "Hello".isAlphanumeric() == true
- "Hello In English".isAlphanumeric() == false
- "HelloInEnglish".isAlphanumeric() == true
- "BonjourEnFrançais".isAlphanumeric() == true
Add Comment
Please, Sign In to add comment