How to implement a regex for password validation in Swift?

前端 未结 6 1498
刺人心
刺人心 2020-12-22 16:18

I want to implement a regex validaton for passwords in Swift? I have tried the following regex, but not successful

([(0-9)(A-Z)(!@#$%ˆ&*+-=<>)]+)([         


        
6条回答
  •  长情又很酷
    2020-12-22 17:00

    func isValidPassword() -> Bool {
        // least one uppercase,
        // least one digit
        // least one lowercase
        // least one symbol
        //  min 8 characters total
        let password = self.trimmingCharacters(in: CharacterSet.whitespaces)
        let passwordRegx = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&<>*~:`-]).{8,}$"
        let passwordCheck = NSPredicate(format: "SELF MATCHES %@",passwordRegx)
        return passwordCheck.evaluate(with: password)
    
    }
    

提交回复
热议问题