iOS 中的电子邮件验证

发布于 2024-12-21 16:53:57 字数 190 浏览 0 评论 0原文

iPhone 编程中的电子邮件验证检查可以使用 RegexKitLite 库 (iOS2.0)、NSPredicate(iOS 3.0 及以上版本)和 NSRegularExpression (iOS 4.0) 来完成。但任何人都可以说出其中一种相对于另一种的优势是什么,以及哪一种是上述三种选择中最好的验证选项。

Email validation checking in iPhone programming can be done using RegexKitLite library (iOS2.0), NSPredicate (iOS 3.0 onwards) and NSRegularExpression (iOS 4.0). But can anybody state what is the advantage of one over the other and which is the best validating option of the three stated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

谎言月老 2024-12-28 16:53:57

我总是使用 NSPredicate...并且它工作正常

NSString *emailid = emailField.text;
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest =[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL myStringMatchesRegEx=[emailTest evaluateWithObject:emailid];

i am using NSPredicate always...and it is working fine

NSString *emailid = emailField.text;
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest =[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL myStringMatchesRegEx=[emailTest evaluateWithObject:emailid];
多彩岁月 2024-12-28 16:53:57

我的答案是此链接中提供的优秀解决方案的重构答案。

///Returns YES (true) if EMail is valid
+(BOOL) IsValidEmail:(NSString *)emailString Strict:(BOOL)strictFilter
{
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";

    NSString *emailRegex = strictFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

    return [emailTest evaluateWithObject:emailString];
}

My answer is a refactored one from the excellent solution provided in this link..

///Returns YES (true) if EMail is valid
+(BOOL) IsValidEmail:(NSString *)emailString Strict:(BOOL)strictFilter
{
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";

    NSString *emailRegex = strictFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

    return [emailTest evaluateWithObject:emailString];
}
谢绝鈎搭 2024-12-28 16:53:57
- (BOOL)validateEmail:(NSString *)emailStr
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+]+@[A-Za-z0-9.]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:emailStr];
}

On Button Click :

if (![self validateEmail:[txtEmail text]])
    {
        alertValidator.message = @"Please Enter Valid Email Address !";
        [alertValidator show];
        txtEmail.text = @"";
        [txtEmail becomeFirstResponder];
    }
- (BOOL)validateEmail:(NSString *)emailStr
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+]+@[A-Za-z0-9.]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:emailStr];
}

On Button Click :

if (![self validateEmail:[txtEmail text]])
    {
        alertValidator.message = @"Please Enter Valid Email Address !";
        [alertValidator show];
        txtEmail.text = @"";
        [txtEmail becomeFirstResponder];
    }
南烟 2024-12-28 16:53:57

我认为最好的选择,毫无疑问,就是 Mailgun 是免费的电子邮件验证 API。我为它编写了一个简单的 Objective-C 包装器:

https://github.com/benzguo/BZGMailgunEmailValidation

BZGMailgunEmailValidator *validator = 
    [BZGMailgunEmailValidator validatorWithPublicKey:YOUR_PUBLIC_KEY];

[validator validateEmailAddress:self.emailFieldCell.textField.text
                        success:^(BOOL isValid, NSString *didYouMean) {
                        // Validation succeeded
                      } failure:^(NSError *error) {
                        // Validation failed
                      }];

如果出现连接错误,验证器将回退到基于正则表达式的验证。

如果您仍想使用正则表达式验证电子邮件,请查看:

http://www.regular- Expressions.info/email.html

https://wiki.mozilla.org/TLD_List

I think the best option, hands down, is Mailgun's free email validation API. I've written a simple objective-C wrapper for it:

https://github.com/benzguo/BZGMailgunEmailValidation

BZGMailgunEmailValidator *validator = 
    [BZGMailgunEmailValidator validatorWithPublicKey:YOUR_PUBLIC_KEY];

[validator validateEmailAddress:self.emailFieldCell.textField.text
                        success:^(BOOL isValid, NSString *didYouMean) {
                        // Validation succeeded
                      } failure:^(NSError *error) {
                        // Validation failed
                      }];

If there's a connection error, the validator falls back to regex-based validation.

If you still want to validate emails using a regular expression, check out:

http://www.regular-expressions.info/email.html

https://wiki.mozilla.org/TLD_List

救星 2024-12-28 16:53:57

我认为这很大程度上取决于应用程序支持的最低 iOS 版本,如果有内置类可以完成这项工作,请使用它。正如您所暗示的,您将使用其他一些第三方框架以一致的方式支持旧版本。

现在,我会使用 NSPredicateNSRegularExpression,两者都从 iOS 4 开始受支持,如果不是的话,这很可能是新 iOS 应用程序的最低支持版本iOS 5。

有用的帖子

I think it largely depends on the minimum version of iOS an App will support, and if there's a built-in class that'll do the job, use it. As you hinted, you'd use some other 3rd-party framework to support older versions in a consistent way.

Right now, I'd use NSPredicate or NSRegularExpression, both are supported from iOS 4 onwards, which is quite likely to be the minimum supported version for new iOS Apps, if not iOS 5.

Useful post.

灵芸 2024-12-28 16:53:57

完成电子邮件验证。试试这个

- (BOOL)validateEmailWithString:(NSString*)checkString
{

    NSString *laxString = @".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", laxString];
    return [emailTest evaluateWithObject:checkString];
}

complete email validation. try this

- (BOOL)validateEmailWithString:(NSString*)checkString
{

    NSString *laxString = @".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", laxString];
    return [emailTest evaluateWithObject:checkString];
}
赢得她心 2024-12-28 16:53:57

如何在 ios 中创建电子邮件 ID 和密码验证

快点,轻松进行简单的电子邮件 ID 验证,检查代码是否有用。

- (IBAction)actLogin:(id)sender

{

    if([self validateEmailWithString:userNameText.text]==YES)
    {
         NSLog(@"valid email");
         if (passWordText.text.length>6)
         {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:userNameText.text forKey:@"username"];
            [defaults setObject:passWordText.text forKey:@"password"];
            [defaults setObject:@"YES" forKey:@"islogin"];
            [defaults synchronize];
            NSLog(@"Login is Successfully !!");
         }
    }
    else
    {
        NSLog(@"invalid email and password");
    }
}

所以,我希望对你有帮助。
谢谢...

how to create email id and password validation in ios

Hurry up, Easy for simple Validation of Email id check that after code useful.

- (IBAction)actLogin:(id)sender

{

    if([self validateEmailWithString:userNameText.text]==YES)
    {
         NSLog(@"valid email");
         if (passWordText.text.length>6)
         {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:userNameText.text forKey:@"username"];
            [defaults setObject:passWordText.text forKey:@"password"];
            [defaults setObject:@"YES" forKey:@"islogin"];
            [defaults synchronize];
            NSLog(@"Login is Successfully !!");
         }
    }
    else
    {
        NSLog(@"invalid email and password");
    }
}

so,I Hope for helpful you.
Thanks...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文