Guest User

Untitled

a guest
Oct 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. extension String {
  2. var isAlphanumeric: Bool {
  3. return !isEmpty && range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil
  4. }
  5. }
  6.  
  7. "".isAlphanumeric // false
  8. "abc123".isAlphanumeric // true
  9. "iOS 9".isAlphanumeric // false
  10.  
  11. if yourString.rangeOfCharacter(from: CharacterSet.alphanumerics.inverted) != nil {
  12. return "Username can only contain numbers or digits"
  13. }
  14.  
  15. extension String {
  16. func isAlphanumeric() -> Bool {
  17. return self.rangeOfCharacter(from: CharacterSet.alphanumerics.inverted) == nil
  18. }
  19. }
  20.  
  21. "Hello".isAlphanumeric() == true
  22. "Hello In English".isAlphanumeric() == false
  23. "HelloInEnglish".isAlphanumeric() == true
  24. "BonjourEnFrançais".isAlphanumeric() == true
Add Comment
Please, Sign In to add comment