Don_Mag icon

Untitled

Don_Mag | PRO | 04/29/21 12:48:22 PM UTC (Edited) | 0 ⭐ | 958 👁️ | Never ⏰ | []
Swift |

1.85 KB

|

None

|

0 👍

/

0 👎

// ---------------------------------------------------------
// with VC - run in Playground, Simulator, Device
// ---------------------------------------------------------
 
class RXViewController : UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let strs: [String] = [
            "abcd",
            "abcdabcd",
            "abcdAbcd",
            "abcd1bcd",
        ]
        
        strs.forEach { s in
            print(s, validatePassword(password: s))
        }
        
    }
    
    private func validatePassword(password : String) -> Bool {
        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}$"#)
    }
    
}
 
public extension String {
    func regexMatches(regex : String) -> Bool {
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: self)
    }
}
 
// ---------------------------------------------------------
// playground without VC
// ---------------------------------------------------------
 
import Foundation
 
let strs: [String] = [
    "abcd",
    "abcdabcd",
    "abcdAbcd",
    "abcd1bcd",
]
 
strs.forEach { s in
    print(s, validatePassword(password: s))
}
 
private func validatePassword(password : String) -> Bool {
    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}$"#)
}
 
public extension String {
    func regexMatches(regex : String) -> Bool {
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: self)
    }
}
 
// ---------------------------------------------------------------------
// output is the same with VC in Playground, Simulator, Device
// as well as Playground Without VC
 
// abcd false
// abcdabcd false
// abcdAbcd true
// abcd1bcd true
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎