根据正则表达式,电子邮件验证是错误的 [英] Email Validation is wrong as per Regex
问题描述
我在项目中使用电子邮件验证,方法如下所示
I am using email validation into my project which method is like below
//MARK: isValidEmailID
func isValidEmail(testStr:String) -> Bool {
print("validate emilId: \(testStr)")
let emailRegEx = "^(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?(?:(?:(?:[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+(?:\\.[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+)*)|(?:\"(?:(?:(?:(?: )*(?:(?:[!#-Z^-~]|\\[|\\])|(?:\\\\(?:\\t|[ -~]))))+(?: )*)|(?: )+)\"))(?:@)(?:(?:(?:[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)(?:\\.[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)*)|(?:\\[(?:(?:(?:(?:(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))\\.){3}(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))))|(?:(?:(?: )*[!-Z^-~])*(?: )*)|(?:[Vv][0-9A-Fa-f]+\\.[-A-Za-z0-9._~!$&'()*+,;=:]+))\\])))(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?$"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
let result = emailTest.evaluateWithObject(testStr)
return result
}
OR
func isValidEmailID(email: String) -> Bool {
let regExPattern: String = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailValidator: NSPredicate = NSPredicate(format: "SELF MATCHES %@", regExPattern)
let isValid: Bool = emailValidator.evaluateWithObject(email)
return isValid
}
当我输入 modijecky@gmail.com时,这两个正则表达式都可以正常工作,或者其他任何错误的输入,但当我输入 modijecky@gmail.com.com时将不起作用。
This both Regex works fine when I enter "modijecky@gmail.com" or any other wrong input but it will not work when I enter "modijecky@gmail.com.com".
因此,我发现 name @ .com.com是有效的电子邮件地址,并且还有更多类似的子域。因此,现在我希望用户不要输入子域。是否有任何REGEX可以验证一个域名(例如 name@gmail.com)中的电子邮件地址,而不能包含多个域名或子域名。
我也尝试使用与Google不同的Regex并将其实现到项目中,但是会发生相同的问题。
请帮助我。
谢谢
So,I find out that "name@.com.com" is a valid email address and there are more sub-domains like this. So now I want user not to enter sub-domains. Is there any REGEX that validate email address within just one domain like "name@gmail.com" not with multiple domains or sub-domains.
I also try different Regex from google and implement it into project but same problem occurs.
Please help me with it.
Thank you
推荐答案
不要重新发明轮子:
基本上,您可以使用 NSDataDetector
完成繁重的工作,并使所有内容保持一致并更新为工作方式原生在macOS和iOS中。不仅如此,而且还避免了正则表达式的麻烦。
Basically you can use NSDataDetector
to do the heavy lifting and have everything consistent and updated to the way it works in macOS and iOS natively. Not only that but you also avoid regex headaches.
// Simplifying the example from the website a bit
import Foundation
func validate(_ text: String) -> Bool {
let types = NSTextCheckingResult.CheckingType.link.rawValue
guard
let dataDetector = try? NSDataDetector(types: types),
let match = dataDetector
.matches(in: text, options: [], range: NSRangeFromString(text))
.first,
let absoluteString = match.url?.absoluteString
else { return false }
return absoluteString == "mailto:\(text)"
}
validate("test@gmail.com") // -> true
validate(" test@gmail.com") // -> false
这将确保整个文本是一个有效的电子邮件地址,没有多余的字符。
This will make sure that the entire text is a single, valid email address without any superfluous characters.
这篇关于根据正则表达式,电子邮件验证是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!