Java 字符串中电子邮件的非混淆简单验证
我试图找到一种简单的方法来检查用户的输入是否满足电子邮件地址的几个标准。我已经阅读了有关此主题的许多帖子,大多数帖子似乎也想验证电子邮件地址。我并不是想构建一些超级骗子的电子邮件地址验证器/检查器。我正在尝试构建一个方法来检查这些内容:
- 用户输入的字符串包含“@”符号。
- “@”符号之前至少有两个字符。
- 有一个‘.’ at 符号后仅跟三个字符。域名可以任意长,但字符串必须以“._ _ _”结尾。如“.com”或“.net”...
我知道这不是一个包罗万象的电子邮件地址检查器。但这不是我想要的。我想要的就是这么简单的东西。我知道这可能是一个常规问题,但即使在阅读了所有验证电子邮件地址的非常疯狂的方法之后我也无法弄清楚。
这是我到目前为止的代码:(别担心,我已经知道这很可悲......)
public static void checkEmail()
{
validEmail(emailAddresses);
if(validEmail(emailAddresses))
{
}
}
public static boolean validEmail(String email) {
return email.matches("[A-Z0-9._%+-][A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{3}");
}
I am trying to find a simple method to check to see if a user's input meets a couple criteria for an email address. I've read through many threads on this subject and most seem to want to validate the email address too. I'm not trying to build some super duper email address validator/checker. I'm trying to build a method that checks for these things:
- The string entered by the user contains the '@' sign.
- There are at least two characters before the '@' sign.
- There is a '.' after the at sign followed by only three characters. The domain name can be as long as needed, but the string must end with "._ _ _". As in ".com" or ".net"...
I understand that this is not an all encompassing email address checker. That's not what I want though. I want just something this simple. I know that this is probably a routine question but I can't figure it out even after reading all of the seriously crazy ways of validating an email address.
This is the code I have so far: (Don't worry I already know it's pretty pathetic.... )
public static void checkEmail()
{
validEmail(emailAddresses);
if(validEmail(emailAddresses))
{
}
}
public static boolean validEmail(String email) {
return email.matches("[A-Z0-9._%+-][A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{3}");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
javax.mail
包专门为此提供了一个类:互联网地址
。使用 此构造函数 允许您强制执行 RFC822 合规性。The
javax.mail
package provides a class just for this:InternetAddress
. Use this constructor which allows you to enforce RFC822 compliance.并不完美,但完成了工作。
Not perfect, but gets the job done.
阅读 http://download.oracle.com/javase/ 6/docs/api/java/lang/String.html 有关 String 方法的文档。
Read http://download.oracle.com/javase/6/docs/api/java/lang/String.html for the documentation of the String methods.