Guest User

Untitled

a guest
Jul 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. //
  2. // ValidatesEmail.swift
  3. // Validators
  4. //
  5. // Created by Tanya Berezovsky on 03/06/2018.
  6. // Copyright © 2018 Tanya Berezovsky. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. protocol ValidateEmail {
  12. func isEmailValid(_ email: String) throws
  13. }
  14.  
  15. extension ValidateEmail {
  16. func isEmailValid(_ email: String) throws {
  17. if email.count == 0 {
  18. throw ValidationError.emailError(value: .emptyEmail)
  19. } else if !validateEmail(with: email) {
  20. throw ValidationError.emailError(value: .invalidEmail)
  21. }
  22. }
  23.  
  24. private func validateEmail(with email: String) -> Bool {
  25. let emailRegex = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
  26. let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
  27. return emailTest.evaluate(with: email)
  28. }
  29. }
Add Comment
Please, Sign In to add comment