检查电子邮件地址在 iOS 上是否有效
我正在开发一个 iPhone 应用程序,我需要用户在登录时提供他的电子邮件地址。
检查电子邮件地址是否有效的最佳方法是什么?
Possible Duplicate:
Best practices for validating email address in Objective-C on iOS 2.0?
I am developing an iPhone application where I need the user to give his email address at login.
What is the best way to check if an email address is a valid email address?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
良好的可可函数:
宽松与严格的讨论 - http ://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/
因为类别更好,您还可以添加一个接口:
实现
然后利用:
Good cocoa function:
Discussion on Lax vs. Strict - http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/
And because categories are just better, you could also add an interface:
Implement
And then utilize:
要检查字符串变量是否包含有效的电子邮件地址,最简单的方法是根据正则表达式对其进行测试。 regular-expressions.info 对各种正则表达式及其权衡进行了很好的讨论。
这是一个相对简单的方法,倾向于允许一些无效地址通过:
^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[AZ ]{2,6}$
如何使用正则表达式取决于您所使用的 iOS 版本。
iOS 4.x 及更高版本
您可以使用
NSRegularExpression
,它允许您直接针对正则表达式进行编译和测试。iOS 3.x
不包含
NSRegularExpression
类,但包含NSPredicate
,它可以与正则表达式匹配。请访问 cocoawithlove.com。
iOS 2.x
在 Cocoa 库中不包含任何正则表达式匹配。但是,您可以轻松地将 RegexKit Lite 包含在您的项目中,这样您就可以访问 C 级正则表达式 API包含在 iOS 2.0 中。
To check if a string variable contains a valid email address, the easiest way is to test it against a regular expression. There is a good discussion of various regex's and their trade-offs at regular-expressions.info.
Here is a relatively simple one that leans on the side of allowing some invalid addresses through:
^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$
How you can use regular expressions depends on the version of iOS you are using.
iOS 4.x and Later
You can use
NSRegularExpression
, which allows you to compile and test against a regular expression directly.iOS 3.x
Does not include the
NSRegularExpression
class, but does includeNSPredicate
, which can match against regular expressions.Read a full article about this approach at cocoawithlove.com.
iOS 2.x
Does not include any regular expression matching in the Cocoa libraries. However, you can easily include RegexKit Lite in your project, which gives you access to the C-level regex APIs included on iOS 2.0.
这是一个很好的 NSRegularExpression ,对我有用。
[text rangeOfString:@"^.+@.+\\..{2,}$" options:NSRegularExpressionSearch].location != NSNotFound;
你可以插入任何你想要的正则表达式,但我喜欢能够在一行中完成。
Heres a good one with NSRegularExpression that's working for me.
[text rangeOfString:@"^.+@.+\\..{2,}$" options:NSRegularExpressionSearch].location != NSNotFound;
You can insert whatever regex you want but I like being able to do it in one line.
要验证电子邮件字符串,您需要编写正则表达式来检查其形式是否正确。网络上有很多地址,但要小心,因为有些地址可能会排除真正的合法地址。
本质上它看起来像这样
实际上检查电子邮件是否存在并且不退回意味着发送一封电子邮件并查看结果是什么。即它反弹或不反弹。但是,它可能在几个小时内不会反弹或根本不会反弹,并且仍然不是“真实”的电子邮件地址。有许多服务声称可以为您做到这一点,并且可能会由您付费,坦率地说,为什么要费心去看看它是否真实呢?
最好检查用户没有拼写错误他们的电子邮件,否则他们可能会错误地输入电子邮件,而没有意识到这一点,然后因不回复而受到您的攻击。然而,如果有人想添加一个错误的电子邮件地址,没有什么可以阻止他们在 hotmail 或 yahoo(或许多其他地方)上创建它以达到相同的目的。
因此,执行正则表达式并验证结构,但忘记针对服务进行验证。
to validate the email string you will need to write a regular expression to check it is in the correct form. there are plenty out on the web but be carefull as some can exclude what are actually legal addresses.
essentially it will look something like this
Actually checking if the email exists and doesn't bounce would mean sending an email and seeing what the result was. i.e. it bounced or it didn't. However it might not bounce for several hours or not at all and still not be a "real" email address. There are a number of services out there which purport to do this for you and would probably be paid for by you and quite frankly why bother to see if it is real?
It is good to check the user has not misspelt their email else they could enter it incorrectly, not realise it and then get hacked of with you for not replying. However if someone wants to add a bum email address there would be nothing to stop them creating it on hotmail or yahoo (or many other places) to gain the same end.
So do the regular expression and validate the structure but forget about validating against a service.