简体   繁体   中英

How to combine 2 bools together in Swift 4

I'm trying to create a email validation that validates where there is character @ in the email and characters before and after @ . I'm able to this but I can't be able to combine both together. So far I have a computed property that just checks if the email contains @ . How can I combine this together to check all conditions.

extension String {

    var isEmailFormat: Bool {
        if let range = self.range(of: "@") {
            _ = self[(self.startIndex)..<range.lowerBound]
            _ = self[range.upperBound...]
        }
        return self.contains("@")
    }
}


if !self.emailField.text!.isEmailFormat {
                self.addErrorMessage("Invalid email address")
}

Additional (boolean) clauses are separated from the optional binding with a comma:

if let range = self.range(of: "@"), condition1, condition2 { ... }

However, assiging a slice to _ does not test anything, it should be something like this:

var isEmailFormat: Bool {
    if let range = self.range(of: "@"),
        range.lowerBound > startIndex,
        range.upperBound < endIndex {
        return true
    } else {
        return false
    }
}

Alternatively:

var isEmailFormat: Bool {
    if let range = self.range(of: "@") {
        return range.lowerBound > startIndex && range.upperBound < endIndex
    } else {
        return false
    }
}

It's better to use a regular expression in cases like this.

func isValidEmail(testStr:String) -> Bool {        
    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    return emailTest.evaluate(with: testStr)
}

If you want to use 2 booleans, use a return statement like

return condition1 && condition2

but I'm assuming you already know that.

Try this way

var isEmailFormat: Bool {
    if let range = self.range(of: "@"),
       range.lowerBound > self.startIndex,
       range.upperBound < self.endIndex {
        return true
    } else {
        return false
    }
}

Logic behind removing the contains logic

If the string itself not containing the @ character, then the range would be deliberately nil . So not required in this case.

    //Iam using Custom validator 
         func validatorEmail(TF1:UITextField,errorMsg:String = validatorConstants.errorMsg ,errorMsgEmail:String = validatorConstants.emailMsg,fieldName:String = "Email" ) -> Bool {
            var error = validatorConstants.errorMsg
            if fieldName.count > 0 {
              error = validatorConstants.customMsg + fieldName
            }

            if  TF1.text?.isEmpty == true{
             // Any Notification
              return false
            }
            if  TF1.text?.isValidEmail == false{
              // Any Notification
              return false
            }
            return true
          }


  var isValidEmail: Bool {
    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: self)
}   

              // call  this function as
       guard validator.validators(TF1: self.txtfied,fieldName: "FirstName") == false
          else {
           return
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM