Skip to content

Commit

Permalink
Merge pull request #2 from bdolewski/compliant_to_rfc_5322
Browse files Browse the repository at this point in the history
Compliant to RFC 5322
  • Loading branch information
Bartosz Dolewski authored Sep 22, 2019
2 parents fb3b73a + 4f64d73 commit 6cb1140
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SwiftEmailValidator
Very small e-mail validation helper written in Swift. It contains regular expresion that is compliant with RFC 2822.
Very small e-mail validation helper written in Swift. It contains regular expresion that is compliant with RFC 5322.
You can use it via Swift Package Manager or just copy file `SwiftEmailValidator.swift` into your project - done!

## How do I use it in my code?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import Foundation
// MARK: - Namespace EmailValidating
struct EmailValidator {

/// Check if given e-mail adres is correct (compliant to RFC 2822)
/// Check if given e-mail adres is correct (compliant to RFC 5322)
///
/// - Parameter email: e-mail adress to be validated
/// - Returns: true if e-mail is valid, false otherwise
static func isValid(email: String?) -> Bool {
guard let email = email else { return false }

let emailRegex = "(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"+"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"+"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"+"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"+"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"+"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"+"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"
let emailRegex = #"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"#

let emailPredicate = NSPredicate(format: "SELF MATCHES[c] %@", emailRegex)
return emailPredicate.evaluate(with: email)
Expand Down

0 comments on commit 6cb1140

Please sign in to comment.