0

There is an official way to validate if an email address is valid without using regex?

I've already implemented this validation with regex:

public func validate(email:String)->Bool{
    let regex:String = "\\A[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\z"

    let emailTest = NSPredicate(format:"SELF MATCHES %@", regex)

    return emailTest.evaluateWithObject(email)

}

I'm on this for a while and I'm looking for an official way to do this in swift/ios without writing my own logic, which in my opinion is more related to have bugs.

Cedulio Cezar
  • 288
  • 3
  • 10
  • 2
    Only way to reliably verify an email address is to send a confirmation mail. – luk2302 Jun 29 '16 at 17:19
  • I mean validate the format. – Cedulio Cezar Jun 29 '16 at 17:21
  • You basically can't. Check for an "@" and some characters after and before it, everything else is potentially valid. – luk2302 Jun 29 '16 at 17:22
  • It's extremely hard to validate email addresses in regex. See [this article](https://davidcel.is/posts/stop-validating-email-addresses-with-regex/) to understand why. – Code Different Jun 29 '16 at 17:22
  • 1
    I agree with @luk2302, have a look at this article: [Stop Validating Email Addresses with Regex](https://davidcel.is/posts/stop-validating-email-addresses-with-regex/) – Kametrixom Jun 29 '16 at 17:22
  • I don't agree with the decision of marking this question as duplicate. Can someone please reopen it? I am also looking for a way of validating email formats because our App must work offline. – Evandro Pomatti Jun 29 '16 at 18:04
  • @BonanzaOne as I said previously: check for an @ and check that there are *characters* after and before it. Alternatively try this regex: https://www.debuggex.com/r/aH_x42NflV8G-GS7 - not sure if that actually covers everything – luk2302 Jun 29 '16 at 20:57

0 Answers0