Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ---------------------------------------------------------
- // 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
Add Comment
Please, Sign In to add comment