#19: Validate Email Addresses in Swift

There is no perfect email regex. The real validation step is whether the address can actually receive mail, so the safest approach is a simple client-side syntax check followed by server-side verification.

A pragmatic local check can look like this:

func isValidEmail(_ value: String) -> Bool {
    let pattern = #"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"#
    return value.range(of: pattern, options: [.regularExpression, .caseInsensitive]) != nil
}

isValidEmail("me@example.com") // true
isValidEmail("not-an-email")   // false

This accepts the most common real-world addresses without trying to implement the full RFC grammar in your app.

Treat this as an early user-feedback step only. Disposable domains, typos, and unreachable mailboxes still need to be caught on the backend or by an email confirmation flow.


I'm also on Twitter and GitHub.

Subscribe via RSS.