Guest User

Untitled

a guest
Jul 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. protocol ValidatePassword {
  2. func isPasswordValid(_ password: String) throws
  3. }
  4.  
  5. extension ValidatePassword {
  6. func isPasswordValid(_ password: String) throws {
  7. if password.count == 0 {
  8. throw ValidationError.passwordError(value: .emptyPassword)
  9. } else if password.count < 6 || password.count > 10 {
  10. throw ValidationError.passwordError(value: .incorectPasswordLong)
  11. } else if !validatePassword(with: password) {
  12. throw ValidationError.passwordError(value: .passwordHasInvalidCharacters)
  13. }
  14. }
  15. private func validatePassword(with password: String) -> Bool {
  16. let passwordRegEx = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"
  17. let passwordTest = NSPredicate(format:"SELF MATCHES %@", passwordRegEx)
  18. return passwordTest.evaluate(with: password)
  19. }
  20. }
Add Comment
Please, Sign In to add comment