Regex to validate password strength

后端 未结 11 957
天涯浪人
天涯浪人 2020-11-22 02:13

My password strength criteria is as below :

  • 8 characters length
  • 2 letters in Upper Case
  • 1 Special Character (!@#$&*)
  • <
11条回答
  •  余生分开走
    2020-11-22 03:04

    Answers given above are perfect but I suggest to use multiple smaller regex rather than a big one.
    Splitting the long regex have some advantages:

    • easiness to write and read
    • easiness to debug
    • easiness to add/remove part of regex

    Generally this approach keep code easily maintainable.

    Having said that, I share a piece of code that I write in Swift as example:

    struct RegExp {
    
        /**
         Check password complexity
    
         - parameter password:         password to test
         - parameter length:           password min length
         - parameter patternsToEscape: patterns that password must not contains
         - parameter caseSensitivty:   specify if password must conforms case sensitivity or not
         - parameter numericDigits:    specify if password must conforms contains numeric digits or not
    
         - returns: boolean that describes if password is valid or not
         */
        static func checkPasswordComplexity(password password: String, length: Int, patternsToEscape: [String], caseSensitivty: Bool, numericDigits: Bool) -> Bool {
            if (password.length < length) {
                return false
            }
            if caseSensitivty {
                let hasUpperCase = RegExp.matchesForRegexInText("[A-Z]", text: password).count > 0
                if !hasUpperCase {
                    return false
                }
                let hasLowerCase = RegExp.matchesForRegexInText("[a-z]", text: password).count > 0
                if !hasLowerCase {
                    return false
                }
            }
            if numericDigits {
                let hasNumbers = RegExp.matchesForRegexInText("\\d", text: password).count > 0
                if !hasNumbers {
                    return false
                }
            }
            if patternsToEscape.count > 0 {
                let passwordLowerCase = password.lowercaseString
                for pattern in patternsToEscape {
                    let hasMatchesWithPattern = RegExp.matchesForRegexInText(pattern, text: passwordLowerCase).count > 0
                    if hasMatchesWithPattern {
                        return false
                    }
                }
            }
            return true
        }
    
        static func matchesForRegexInText(regex: String, text: String) -> [String] {
            do {
                let regex = try NSRegularExpression(pattern: regex, options: [])
                let nsString = text as NSString
                let results = regex.matchesInString(text,
                    options: [], range: NSMakeRange(0, nsString.length))
                return results.map { nsString.substringWithRange($0.range)}
            } catch let error as NSError {
                print("invalid regex: \(error.localizedDescription)")
                return []
            }
        }
    }
    

提交回复
热议问题