如何在文本字段上使用以下代码验证(在swift中)

时间:2015-04-27 11:01:16

标签: validation swift email

 func isValidEmail(testStr:String) -> Bool {
    // println("validate calendar: \(testStr)")
    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"

    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    return emailTest.evaluateWithObject(testStr)
}

有谁知道如何在Swift中验证电子邮件地址?我找到了这段代码:

1 个答案:

答案 0 :(得分:0)

假设txtEmail是您的UITextField:

var isValid = isValidEmail(txtEmail.text)
if isValid {
  // Do some things when email is valid
}
else {
  // Show error to user
}