Created
May 30, 2017 23:59
-
-
Save dimohamdy/bd64142bea1c00552ea6ee6ab9c0335c to your computer and use it in GitHub Desktop.
check if password is strong using swift
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
/* | |
check if the password lenght more than or equal 8 | |
and have lowercase , uppercase ,decimalDigits and special characters like !@#$%^&*()_-+ is optional | |
Why i not use regular expression ? | |
Because it's difficult to support reserved characters in regular expression syntax. | |
*/ | |
func isValidated(_ password: String) -> Bool { | |
var lowerCaseLetter: Bool = false | |
var upperCaseLetter: Bool = false | |
var digit: Bool = false | |
var specialCharacter: Bool = false | |
if password.characters.count >= 8 { | |
for char in password.unicodeScalars { | |
if !lowerCaseLetter { | |
lowerCaseLetter = CharacterSet.lowercaseLetters.contains(char) | |
} | |
if !upperCaseLetter { | |
upperCaseLetter = CharacterSet.uppercaseLetters.contains(char) | |
} | |
if !digit { | |
digit = CharacterSet.decimalDigits.contains(char) | |
} | |
if !specialCharacter { | |
specialCharacter = CharacterSet.punctuationCharacters.contains(char) | |
} | |
} | |
if specialCharacter || (digit && lowerCaseLetter && upperCaseLetter) { | |
//do what u want | |
return true | |
} | |
else { | |
return false | |
} | |
} | |
return false | |
} | |
let isVaildPass:Bool = isValidated("Test**00+-") | |
print(isVaildPass) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment