0
 let email  = capgEmailTextField.text
 func isEmailValid(str :String) -> Bool {
      let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z]+\\.[A-Za-z]{2,4}"
      let testMail = NSPredicate(format : "SELF MATCHES %@", emailRegEx)
      return testMail.evaluate(with : str)
            }
            
          
  if !isEmailValid(str: email!){
           
     print("Email is not valid")
                
     let alert = UIAlertController(title: "Note", message: "Email is not valid", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Okay", style: .cancel, handler: nil))
    present(alert, animated: true, completion: nil)
    }
  else if isEmailValid(str: email!){
     print("Emailvalid so m saving")
   do{
   //  AppDelegate.shared().saveContext()
       let alert = UIAlertController(title: "Note", message: "Details submitted", preferredStyle: .alert)
 alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
    present(alert, animated: true, completion: nil)
                    }

It enters the if statement and checks for the condition but saves anyways . Doesn't not enter else statement. But when save context was commented it got saved so I don't know how its getting saved

  • This question doesn't make sense, `email` is just a string so how can it be saved into your Core Data store? I assume there is some important code missing here. – Joakim Danielson Dec 31 '21 at 14:31
  • Side note, your email regex doesn’t allow emails in non-English alphabets, which are probably the majority of the emails out there, lol – Alexander Dec 31 '21 at 15:10
  • // create objects and save it let employee = Employee(context: context) employee.setValue(nameTextField.text, forKey: "name") employee.setValue(empIDTextField.text, forKey:"empID") employee.setValue(empEmailTextField.text, forKey: "empEmail") This is the object I'm creating.And email is just an attribute for which I added validation it checks the condition fine but saves the wrong format of email anyways. – Rachael S Jan 03 '22 at 10:07

0 Answers0