Skip to content

Instantly share code, notes, and snippets.

@dimohamdy
Created May 30, 2017 23:59
Show Gist options
  • Save dimohamdy/bd64142bea1c00552ea6ee6ab9c0335c to your computer and use it in GitHub Desktop.
Save dimohamdy/bd64142bea1c00552ea6ee6ab9c0335c to your computer and use it in GitHub Desktop.
check if password is strong using swift
/*
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