2

My password strength criteria is as below :

8 characters length No Special Characters Atleast 1 numeral Atleast 1 alphabet

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
FH-
  • 133
  • 1
  • 9

2 Answers2

9

update: Xcode 8.3.2 • Swift 3.1

enum PasswordError: String, Error {
    case eightCharacters
    case oneUppercase
    case oneLowercase
    case oneDecimalDigit
}

extension String {
    func validatePassword() throws  {
        guard count > 7
            else { throw PasswordError.eightCharacters }
        guard rangeOfCharacter(from: .uppercaseLetters) != nil
            else { throw PasswordError.oneUppercase }
        guard rangeOfCharacter(from: .lowercaseLetters) != nil
            else { throw PasswordError.oneLowercase }
        guard rangeOfCharacter(from: .decimalDigits) != nil
            else { throw PasswordError.oneDecimalDigit }
    }
}

let myPass = "12345678"

do {
    try myPass.validatePassword()
    print("valid password action")
} catch let error as PasswordError {
    print("Password error:", error) 
    switch error {
    case .eightCharacters:
        print("Needs At Least Eight Characters action")
    case .oneUppercase:
        print("Needs At Least one Uppercase action")
    case .oneLowercase:
        print("Needs At Least one Lowercase action")
    case .oneDecimalDigit:
        print("Needs At Least One DecimalDigit action")
    }
} catch {
    print("error:", error)
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
2

Try this:

^(?=.*\d)(?=.*[a-zA-Z]).{8,}$

or

^(?=.*\d)(?=.*[a-zA-Z])(?!.*[\W_\x7B-\xFF]).{8,}$

The first does not care about special characters, the second disallows them.

MirekE
  • 11,515
  • 5
  • 35
  • 28
  • I realize the OP specifically mentions using a regex in the title, but I’m pretty sure they don’t actually want to be using a regex for this task (because it’s not going to enable them to emit any specific/useful error messages if the password fails against the criteria). – sideshowbarker Aug 12 '15 at 01:24
  • @sideshowbarker You can let the user enter the whole password and then validate it, in this case detailed message is great. Or you can validate the password after each typed character and give some visual feedback (different color of the text, for example) to show if the password is strong enough. In that case detailed error message seems excessive. – MirekE Aug 12 '15 at 01:46
  • Thank you for the answer! What I did in my error message is display the criteris for a valid password. I think thats good enough. Thank you guys! – FH- Aug 17 '15 at 07:30
  • 1
    You'll need to escape the \d and \W etc. For example: \\d and \\W and so on. You could also improve the performance of the above by matching each part of the pattern the minimum number of times by replacing '.*' with '.?+'. Take a look at the cheat sheet of the Ray Wenderlich tutorial at http://cdn5.raywenderlich.com/downloads/RW-NSRegularExpression-Cheatsheet.pdf – zevij Nov 13 '15 at 11:26
  • Can not see any iOs related string syntax there – Juan Boero May 11 '16 at 16:00