Skip to content

Instantly share code, notes, and snippets.

@RPallas92
Last active September 20, 2017 15:26
Show Gist options
  • Save RPallas92/f8069bff9c5c24b14e3cf524f3c0b611 to your computer and use it in GitHub Desktop.
Save RPallas92/f8069bff9c5c24b14e3cf524f3c0b611 to your computer and use it in GitHub Desktop.
func validatePassword(username: String, password:String) -> [String]{
var errors:[String] = []
if password.characters.count < 8 {
errors.append("Password must have more than 8 characters.")
}
if (password.range(of:"[\\W]", options: .regularExpression) == nil){
errors.append("Password must contain a special character.")
}
if (username == password){
errors.append("Username and password MUST be different.")
}
return errors
}
validatePassword(username: "Richi", password: "Richi")
/*
* Array<String> 3 elements:
- 0: "Password must have more than 8 characters."
- 1: "Password must contain a special character."
- 2: "Username and password MUST be different."
*/
validatePassword(username: "Richi", password: "Ricardo$")
/*
* Array<String> 0 elements
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment