StringUtil.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // StringUtil.swift
  3. // CommonUtil
  4. //
  5. // Created by lijunjie on 15/11/23.
  6. // Copyright © 2015年 lijunjie. All rights reserved.
  7. //
  8. import Foundation
  9. enum Validate {
  10. case email(_: String)
  11. case phoneNum(_: String)
  12. case carNum(_: String)
  13. case username(_: String)
  14. case password(_: String)
  15. case nickname(_: String)
  16. case URL(_: String)
  17. case IP(_: String)
  18. var isRight: Bool {
  19. var predicateStr:String!
  20. var currObject:String!
  21. switch self {
  22. case let .email(str):
  23. predicateStr = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
  24. currObject = str
  25. case let .phoneNum(str):
  26. predicateStr = "^((13[0-9])|(15[^4,\\D]) |(17[0,0-9])|(18[0,0-9]))\\d{8}$"
  27. currObject = str
  28. case let .carNum(str):
  29. predicateStr = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
  30. currObject = str
  31. case let .username(str):
  32. predicateStr = "^[A-Za-z0-9]{6,20}+$"
  33. currObject = str
  34. case let .password(str):
  35. predicateStr = "^[a-zA-Z0-9]{6,20}+$"
  36. currObject = str
  37. case let .nickname(str):
  38. predicateStr = "^[\\u4e00-\\u9fa5]{4,8}$"
  39. currObject = str
  40. case let .URL(str):
  41. predicateStr = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
  42. currObject = str
  43. case let .IP(str):
  44. predicateStr = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
  45. currObject = str
  46. }
  47. let predicate = NSPredicate(format: "SELF MATCHES %@" ,predicateStr)
  48. return predicate.evaluate(with: currObject)
  49. }
  50. }