Reputation: 1527
Following is the regex that I am currently using for validating passwords: at least one uppercase character, at least one lowercase character, at least one number and minimum 8 characters in length.
func isValidPassword() -> Bool {
let passwordRegEx = "^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,}$"
return NSPredicate(format:"SELF MATCHES %@", passwordRegEx).evaluate(with: self)
}
I would now like to include special characters and update the validation rule as follows.
minimum 8 characters in length, should include at least 3 of these: uppercase character, lowercase character, number and special character.
What would be the regex for this requirement?
Upvotes: 0
Views: 1643
Reputation: 10199
I think you better write a for-loop to iterate through the single characters and keep track of which createria has already passed, instead of creating a more and more complex regular expression.
Upvotes: 2