如何限制用户在文本字段中输入电子邮件地址。 问题是,我的警报未显示,只需注册而不检查电子邮件字段是否有效
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!)) {...
表示如果电子邮件有效,将显示无效电子邮件警报。您只需使用 !
if (!isValidEmail(UserEmailTextFiled.text!)) {
反转结果即可
public func isValidEmailAddress() -> Bool {
let emailRegex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"
return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: self)
}
这是带有正则表达式的 Swift 3.0 Func。 只需复制/粘贴您的帮助程序或扩展文件,然后使用它。
在 Swift 3.0 中
extension String {
func nsstring () -> NSString {
return (self as NSString)
}
func isValidEmailAddress () -> Bool
{
var returnValue = true
let emailRegEx = "[A-Z0-9a-z.-_]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,3}"
do {
let regex = try NSRegularExpression(pattern: emailRegEx)
let results = regex.matches(in: self.trimmingCharacters(in: .whitespacesAndNewlines), range: NSRange(location: 0, length: self.characters.count))
if results.count == 0
{
returnValue = false
}
}
catch let error as NSError
{
print("invalid regex: \(error.localizedDescription)")
returnValue = false
}
return returnValue
}
}
在视图控制器中
let isValidEmail = yourtextField?.text?.isValidEmailAddress()
if isValidEmail!
{
//Code Here
}
else
{
//Error Code Here
}
swift 3.0 中的电子邮件验证
@IBOutlet weak var emailid: UILabel!
@IBOutlet weak var MsgAlrtEmailId: UILabel!
@IBAction func Register(_ sender: Any)
{
//email
let providedEmailAddress = emailid.text
let isEmailAddressValid = isValidEmailAddress(emailAddressString: providedEmailAddress!)
if isEmailAddressValid
{
print("Email address is valid")
MsgAlrtEmailId.text = "Email Address is valid"
MsgAlrtEmailId.sizeToFit()
MsgAlrtEmailId.textColor = UIColor.blue
} else {
print("Email address is not valid")
MsgAlrtEmailId.text = "Email Address Not valid"
MsgAlrtEmailId.sizeToFit()
MsgAlrtEmailId.textColor = UIColor.red
}
//email rule validation
func isValidEmailAddress(emailAddressString: String) -> Bool {
var returnValue = true
let emailRegEx = "[A-Z0-9a-z.-_]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,3}"
do {
let regex = try NSRegularExpression(pattern: emailRegEx)
let nsString = emailAddressString as NSString
let results = regex.matches(in: emailAddressString, range: NSRange(location: 0, length: nsString.length))
if results.count == 0
{
returnValue = false
}
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
returnValue = false
}
return returnValue
}
let email = "[email protected]"
if email ~= /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i {
print("Valid email address.")
} else {
print("Invalid email address.")
}