Java 字符串中电子邮件的非混淆简单验证

发布于 2024-11-01 06:58:09 字数 682 浏览 1 评论 0原文

我试图找到一种简单的方法来检查用户的输入是否满足电子邮件地址的几个标准。我已经阅读了有关此主题的许多帖子,大多数帖子似乎也想验证电子邮件地址。我并不是想构建一些超级骗子的电子邮件地址验证器/检查器。我正在尝试构建一个方法来检查这些内容:

  1. 用户输入的字符串包含“@”符号。
  2. “@”符号之前至少有两个字符。
  3. 有一个‘.’ 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:

  1. The string entered by the user contains the '@' sign.
  2. There are at least two characters before the '@' sign.
  3. 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 技术交流群。

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

发布评论

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

评论(3

从﹋此江山别 2024-11-08 06:58:09

javax.mail 包专门为此提供了一个类: 互联网地址。使用 此构造函数 允许您强制执行 RFC822 合规性。

The javax.mail package provides a class just for this: InternetAddress. Use this constructor which allows you to enforce RFC822 compliance.

時窥 2024-11-08 06:58:09

并不完美,但完成了工作。

static boolean validEmail(String email) {
    // editing to make requirements listed
    // return email.matches("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}");
    return email.matches("[A-Z0-9._%+-][A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{3}");
}

void checkEmails() {
    for(String email : emailAddresses) {
        if(validEmail(email)) {
             // it's a good email - do something good with it
        }
        else {
             // it's a bad email - do something... bad to it? sounds dirty...
        }
    }
}

Not perfect, but gets the job done.

static boolean validEmail(String email) {
    // editing to make requirements listed
    // return email.matches("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}");
    return email.matches("[A-Z0-9._%+-][A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{3}");
}

void checkEmails() {
    for(String email : emailAddresses) {
        if(validEmail(email)) {
             // it's a good email - do something good with it
        }
        else {
             // it's a bad email - do something... bad to it? sounds dirty...
        }
    }
}
攀登最高峰 2024-11-08 06:58:09
int indexOfAt = email.indexOf('@');
// first check : 
if (indexOfAt < 0) { 
  // error
}

// second check :
if (indexOfAt < 2) {
  // error
}

// third check :
int indexOfLastDot = email.lastIndexOf('.');
if (indexOfLastDot < indexOfAt || indexOfLastDot != (email.length() - 4)) {
  // error
}

阅读 http://download.oracle.com/javase/ 6/docs/api/java/lang/String.html 有关 String 方法的文档。

int indexOfAt = email.indexOf('@');
// first check : 
if (indexOfAt < 0) { 
  // error
}

// second check :
if (indexOfAt < 2) {
  // error
}

// third check :
int indexOfLastDot = email.lastIndexOf('.');
if (indexOfLastDot < indexOfAt || indexOfLastDot != (email.length() - 4)) {
  // error
}

Read http://download.oracle.com/javase/6/docs/api/java/lang/String.html for the documentation of the String methods.

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