Don_Mag

Untitled

Apr 29th, 2021 (edited)
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.85 KB | None | 0 0
  1. // ---------------------------------------------------------
  2. // with VC - run in Playground, Simulator, Device
  3. // ---------------------------------------------------------
  4.  
  5. class RXViewController : UIViewController {
  6.    
  7.     override func viewDidLoad() {
  8.         super.viewDidLoad()
  9.        
  10.         let strs: [String] = [
  11.             "abcd",
  12.             "abcdabcd",
  13.             "abcdAbcd",
  14.             "abcd1bcd",
  15.         ]
  16.        
  17.         strs.forEach { s in
  18.             print(s, validatePassword(password: s))
  19.         }
  20.        
  21.     }
  22.    
  23.     private func validatePassword(password : String) -> Bool {
  24.         return password.regexMatches(regex: #"^(((?=.*[a-z]{1})(?=.*[A-Z]{1}))|((?=.*\d{1})(?=.*[a-zA-Z]{1}))|((?=.*\W{1})(?=.*[a-zA-Z]{1}))|((?=.*\d{1})(?=.*\W{1}))).{8,20}$"#)
  25.     }
  26.    
  27. }
  28.  
  29. public extension String {
  30.     func regexMatches(regex : String) -> Bool {
  31.         let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
  32.         return predicate.evaluate(with: self)
  33.     }
  34. }
  35.  
  36. // ---------------------------------------------------------
  37. // playground without VC
  38. // ---------------------------------------------------------
  39.  
  40. import Foundation
  41.  
  42. let strs: [String] = [
  43.     "abcd",
  44.     "abcdabcd",
  45.     "abcdAbcd",
  46.     "abcd1bcd",
  47. ]
  48.  
  49. strs.forEach { s in
  50.     print(s, validatePassword(password: s))
  51. }
  52.  
  53. private func validatePassword(password : String) -> Bool {
  54.     return password.regexMatches(regex: #"^(((?=.*[a-z]{1})(?=.*[A-Z]{1}))|((?=.*\d{1})(?=.*[a-zA-Z]{1}))|((?=.*\W{1})(?=.*[a-zA-Z]{1}))|((?=.*\d{1})(?=.*\W{1}))).{8,20}$"#)
  55. }
  56.  
  57. public extension String {
  58.     func regexMatches(regex : String) -> Bool {
  59.         let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
  60.         return predicate.evaluate(with: self)
  61.     }
  62. }
  63.  
  64. // ---------------------------------------------------------------------
  65. // output is the same with VC in Playground, Simulator, Device
  66. // as well as Playground Without VC
  67.  
  68. // abcd false
  69. // abcdabcd false
  70. // abcdAbcd true
  71. // abcd1bcd true
  72.  
Add Comment
Please, Sign In to add comment