ios - iOS 中的电子邮件验证问题?

标签 ios iphone xcode validation ipad

我正在 iOS 中进行电子邮件验证。我正在使用以下代码电子邮件验证。这工作正常,但无法验证以下模式。

.test@gmail.com
test..@gmail.com
test.@tgmail.com
Abc..123@example.com

使用的代码

-(BOOL) IsValidEmail:(NSString *)checkString
{
    BOOL stricterFilter = NO;
    NSString *stricterFilterString = @"^[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$";
    NSString *laxString = @"^.+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];   
}

请告诉我怎么做?

最佳答案

- (BOOL) validateEmail: (NSString *) candidate {
    NSString *emailRegex =
@"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
@"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
@"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
@"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
@"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
@"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
@"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", emailRegex]; 

    return [emailTest evaluateWithObject:candidate];
}

链接:More

enter image description here

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

相关文章:

ios - 如何播放阵列中的特定声音?

android - react 导航 : how to detect if a screen is inside a TabNavigator or a DrawerNavigator?

XCode 如何修复从项目中删除的文件。错误丢失文件

iphone - iOS + 自动布局 : UITableViewCell label + textfield width with rotation

ios - 从容器 View 继续

ios - 架构 i386 的 undefined symbol : trying to add in AVFoundation

ios - 准备 iOS 演示应用程序和主应用程序的最佳实践是什么?

ios - SFSpeechRecognizer在IOS 13.2中无法正常工作

ios - 在捕获列表中使用 unowned 导致崩溃,即使 block 本身没有执行

iphone - iOS/Objective-C : SHA-1 and Base64