60 lines
1.6 KiB
Swift
60 lines
1.6 KiB
Swift
//
|
|
// File.swift
|
|
// WOKA
|
|
//
|
|
// Created by MacBook Pro on 29/04/24.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
// MARK: - This will validate the Email ID, validation result can be modified from the enum
|
|
|
|
struct EmailValidation {
|
|
let emailString: String
|
|
|
|
init(email: String) {
|
|
self.emailString = email
|
|
}
|
|
|
|
private func validate() -> Bool {
|
|
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
|
|
let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
|
|
return emailPred.evaluate(with: emailString)
|
|
}
|
|
|
|
func validate() -> EmailValidatorResult {
|
|
if emailString == "" {
|
|
return .isEmpty
|
|
}
|
|
|
|
if !emailString.contains("@") {
|
|
return .missingAtTheRateSymbol
|
|
}
|
|
|
|
// if !emailDomain.allCases.contains(where: { emailString.contains($0.rawValue) }) {
|
|
// return .missingDotCom
|
|
// }
|
|
|
|
if !validate() {
|
|
return .badlyFormatted
|
|
}
|
|
|
|
return .isCorrect
|
|
}
|
|
}
|
|
|
|
enum EmailValidatorResult: String {
|
|
case missingAtTheRateSymbol = "An Email Address must contain a single '@'"
|
|
case isEmpty = "Please enter your email"
|
|
case isCorrect = "Email address is in correct format"
|
|
case missingDotCom = "Email Address does not contain the domain portion (e.g. '.com')"
|
|
case badlyFormatted = "Please enter a valid Email Address"
|
|
}
|
|
|
|
enum emailDomain : String,CaseIterable{
|
|
case com = ".com"
|
|
case net = ".net"
|
|
case coin = ".co.in"
|
|
case couk = ".co.uk"
|
|
}
|