Created
August 28, 2014 20:01
-
-
Save chamrc/4b74d0f6caa844116150 to your computer and use it in GitHub Desktop.
Swift Password Strength Checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum PasswordStrength: Int { | |
case None | |
case Weak | |
case Moderate | |
case Strong | |
static func checkStrength(password: String) -> PasswordStrength { | |
var len: Int = countElements(password) | |
var strength: Int = 0 | |
switch len { | |
case 0: | |
return .None | |
case 1...4: | |
strength++ | |
case 5...8: | |
strength += 2 | |
default: | |
strength += 3 | |
} | |
// Upper case, Lower case, Number & Symbols | |
let patterns = ["^(?=.*[A-Z]).*$", "^(?=.*[a-z]).*$", "^(?=.*[0-9]).*$", "^(?=.*[!@#%&-_=:;\"'<>,`~\\*\\?\\+\\[\\]\\(\\)\\{\\}\\^\\$\\|\\\\\\.\\/]).*$"] | |
for pattern in patterns { | |
if (password =~ pattern).boolValue { | |
strength++ | |
} | |
} | |
switch strength { | |
case 0: | |
return .None | |
case 1...3: | |
return .Weak | |
case 4...6: | |
return .Moderate | |
default: | |
return .Strong | |
} | |
} | |
} |
@herrstypa =~ looks like overloaded operator
Change line 26 to:
if (password.rangeOfString(pattern, options: .RegularExpressionSearch) != nil) {
strength++
}
Voilà.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How is
if (password =~ pattern).boolValue {
strength++
}
written in swift 2.0?