Created
July 17, 2020 04:58
-
-
Save dilipiOSDeveloper/56b747338b42f4aa6f85a85abdc8d38f to your computer and use it in GitHub Desktop.
email id validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Datatype specifier | |
enum DataType: Int { | |
case Other = 0 // This can be string | |
case Number = 1 | |
case Email = 2 | |
} | |
// Validate Email | |
func isEmail(emailString: String) -> Bool { | |
// Sample regex for email - You can use your own regex for email | |
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}" | |
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegex) | |
return emailTest.evaluate(with: emailString) | |
} | |
// Check Datatype | |
func checkDataType(text: String)-> DataType { | |
if let intVal = text.toInt() { | |
return DataType.Number | |
} else if isEmail(emailString: text) { | |
return DataType.Email | |
} else { | |
return DataType.Other | |
} | |
} | |
// print datatype | |
let dataType = checkDataType(text: "Your Input String") | |
print("DataType = \(dataType)") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment