RegEx pour Alphanumeric SANS caractères spéciaux SWIFT iOs

Mon critère de force de mot de passe est comme ci-dessous:

8 caractères length Aucun caractère spécial Atleast 1 numeral Atleast 1 alphabet

mise à jour: Xcode 8.3.2 • Swift 3.1

enum PasswordError: Ssortingng, Error { case needsAtLeastEightCharacters case needsAtLeastOneLetter case needsAtLeastOneDecimalDigit } extension Ssortingng { func validatePassword() throws { guard characters.count > 7 else { throw PasswordError.needsAtLeastEightCharacters } guard rangeOfCharacter(from: .letters) != nil else { throw PasswordError.needsAtLeastOneLetter } guard rangeOfCharacter(from: .decimalDigits) != nil else { throw PasswordError.needsAtLeastOneDecimalDigit } } } 

 let myPass = "12345678" do { try myPass.validatePassword() print("valid password action") } catch let error as PasswordError { print("Password error:", error) // Password error: needsAtLeastOneLetter switch error { case .needsAtLeastEightCharacters: print("NeedsAtLeastEightCharacters action") case .needsAtLeastOneLetter: print("NeedsAtLeastOneLetter action") // NeedsAtLeastOneLetter action case .needsAtLeastOneDecimalDigit: print("NeedsAtLeastOneDecimalDigit action") } } catch { print("error:", error) } 

Essaye ça:

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

ou

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

Le premier ne se soucie pas des caractères spéciaux, le second les interdit.