0

Is there any way someone can check if the user is creating an account with a valid email address. I am trying to create an app, however, the user can register with any kind of data, even without using the valid email address. Can someone give me an idea, what should I use so that the error comes up if the email address is invalid or if the person is just typing text and not a valid email address. Also, what can I implement to detect if the person is trying to create an account with the used email address.

Answer could be in theory or code. An example of code would be more helpful as it would help me to understand the concept better. Thanks in advance.

Asperi
  • 228,894
  • 20
  • 464
  • 690
Ricky Chhetri
  • 89
  • 1
  • 7
  • 3
    https://stackoverflow.com/questions/25471114/how-to-validate-an-e-mail-address-in-swift – Larme Mar 15 '22 at 09:27

1 Answers1

0
extension String {
func isValidEmail() -> Bool {
    // here, `try!` will always succeed because the pattern is valid
    let regex = try! NSRegularExpression(pattern: "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", options: .caseInsensitive)
    return regex.firstMatch(in: self, options: [], range: NSRange(location: 0, length: count)) != nil
   }  
}

when Use This extension

if "test@mailcom".isValidEmail() {
}
Karan Mehra
  • 339
  • 3
  • 12