gpt4 book ai didi

ios - 电子邮件验证 swift 2

转载 作者:可可西里 更新时间:2023-11-01 00:59:14 25 4
gpt4 key购买 nike

如何限制用户在文本字段中输入电子邮件地址。问题是,我的警报没有显示,只是注册而不检查电子邮件字段是否有效

if ( username.isEmpty || email.isEmpty || password.isEmpty || phonenumper.isEmpty) {

let alert = UIAlertController(title: "Sign Up Failed!", message:"Please enter your data for Signup", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK ", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}

else {
if (isValidEmail(UserEmailTextFiled.text!)) {
let alert = UIAlertController(title: "Inviled Email", message:"Please enter your Email", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK ", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}

}else{
//code

}
func isValidEmail(testStr:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let range = testStr.rangeOfString(emailRegEx, options:.RegularExpressionSearch)
let result = range != nil ? true : false
return result
}

最佳答案

根据您发布的代码,if (isValidEmail(UserEmailTextFiled.text!)) {... 意味着如果电子邮件是 invalid 将显示电子邮件警报有效。您只需要使用 ! if (!isValidEmail(UserEmailTextFiled.text!)) {

反转结果

关于ios - 电子邮件验证 swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37955320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com