Skip to content

Instantly share code, notes, and snippets.

@alfianlosari
Last active June 3, 2018 16:11
Show Gist options
  • Save alfianlosari/cb075c4c146b0fd6d35197d5d539ad9b to your computer and use it in GitHub Desktop.
Save alfianlosari/cb075c4c146b0fd6d35197d5d539ad9b to your computer and use it in GitHub Desktop.
EmailModel
import Foundation
class EmailModel {
static let valueDidChange = Notification.Name("valueDidChange")
static let isValidDidChange = Notification.Name("isValidDidChange")
static private let regexEmail = "[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}"
init(value: String) {
self.value = value
}
var value: String = "" {
willSet {
NotificationCenter.default.post(name: EmailModel.valueDidChange, object: self, userInfo: [\EmailModel.value: newValue])
self.isValid = validateEmail(newValue)
}
}
public private(set) var isValid: Bool = false {
willSet {
NotificationCenter.default.post(name: EmailModel.isValidDidChange, object: self, userInfo: [\EmailModel.isValid: newValue])
}
}
private func validateEmail(_ email: String) -> Bool {
guard !email.isEmpty else { return false }
let emailPredicate = NSPredicate(format: "SELF MATCHES %@", EmailModel.regexEmail)
let emailEvaluate = emailPredicate.evaluate(with: email)
guard emailEvaluate else { return false }
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment