0

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?

Sujal
  • 1,447
  • 19
  • 34
  • 1
    try this `^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$` – Anbu.Karthik Jan 10 '19 at 06:53
  • I'll suggest you create four regexes for each character set and use programming logic to individually check if your password validates and if any three regex validates then true else false. Although I can help with a pure regex solution but the regex would be a little too large and look a bit complicated and unmaintainable. – Pushpesh Kumar Rajwanshi Jan 10 '19 at 08:04
  • You might find [Reference - Password Validation](https://stackoverflow.com/q/48345922/3600709) useful. – ctwheels Mar 27 '19 at 15:26

1 Answers1

2

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.

  • The regex will be hard to understand / maintained by any programmer in just a few month. And any programmer includes you in a few months
  • You could provide detailled information to the user if the requiremens do not match. You could display a message "No uppercase character found in password" etc.
  • You could (more) easily implement stuff like "disallow repeating numbers" and so on
  • Although performance does not matter, regexp will be way slower than looping.
Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34