【问题标题】:Is there email validator code for Java ME or BlackBerry?是否有 Java ME 或 BlackBerry 的电子邮件验证器代码?
【发布时间】:2011-10-18 22:12:41
【问题描述】:

是否有一些适用于 Java ME 或 BlackBerry 的标准电子邮件验证器代码示例?

【问题讨论】:

    标签: validation blackberry java-me


    【解决方案1】:

    使用此代码检查给定的电子邮件 ID 是否有效,

    private static boolean validateEmailID(String email) {
    
            if (email == null || email.length() == 0 || email.indexOf("@") == -1 || email.indexOf(" ") != -1) {
                return false;
            }
            int emailLenght = email.length();
            int atPosition = email.indexOf("@");
    
            String beforeAt = email.substring(0, atPosition);
            String afterAt = email.substring(atPosition + 1, emailLenght);
    
            if (beforeAt.length() == 0 || afterAt.length() == 0) {
                return false;
            }
            if (email.charAt(atPosition - 1) == '.') {
                return false;
            }
            if (email.charAt(atPosition + 1) == '.') {
                return false;
            }
            if (afterAt.indexOf(".") == -1) {
                return false;
            }
            char dotCh = 0;
            for (int i = 0; i < afterAt.length(); i++) {
                char ch = afterAt.charAt(i);
                if ((ch == 0x2e) && (ch == dotCh)) {
                    return false;
                }
                dotCh = ch;
            }
            if (afterAt.indexOf("@") != -1) {
                return false;
            }
            int ind = 0;
            do {
                int newInd = afterAt.indexOf(".", ind + 1);
    
                if (newInd == ind || newInd == -1) {
                    String prefix = afterAt.substring(ind + 1);
                    if (prefix.length() > 1 && prefix.length() < 6) {
                        break;
                    } else {
                        return false;
                    }
                } else {
                    ind = newInd;
                }
            } while (true);
            dotCh = 0;
            for (int i = 0; i < beforeAt.length(); i++) {
                char ch = beforeAt.charAt(i);
                if (!((ch >= 0x30 && ch <= 0x39) || (ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a)
                        || (ch == 0x2e) || (ch == 0x2d) || (ch == 0x5f))) {
                    return false;
                }
                if ((ch == 0x2e) && (ch == dotCh)) {
                    return false;
                }
                dotCh = ch;
            }
            return true;
        }
    

    【讨论】:

    • 为什么需要email.indexOf(" ") != -1 测试? (第一次测试)
    • 因为电子邮件不能有空格
    • 此验证码已完全关闭:例如它不允许在“@”符号之前使用“+”(0x2b),尽管它是有效的......wikipedia 给出了允许的更全面的摘要。电子邮件地址的唯一官方和权威源语法是RFC。我希望那里的任何开发人员在编写如此可怕的代码之前都阅读过这个 RFC。
    • 代码服务于目的,不是“可怕”或“完全关闭”!出于用户注册的目的,您不希望使用带有 + 号的电子邮件地址和 RFC 规定的其他非标准“有效”电子邮件地址。
    【解决方案2】:

    您可以简单地搜索电子邮件验证正则表达式模式。这是检查电子邮件字符串格式的最简单有效的方法。请参阅下面的链接。

    http://www.zparacha.com/ultimate-java-regular-expression-to-validate-email-address/

    http://leshazlewood.com/2006/02/04/java-email-address-validation-the-right-way-regular-expression/

    您会发现许多其他正则表达式示例。对于正则表达式,请查看以下链接。

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

    【讨论】:

    • 正则表达式在 BlackBerry 上不可用。
    【解决方案3】:
    public static boolean validateEmailID(String email) {
        email = email.trim();
        String reverse = new StringBuffer(email).reverse().toString();
        if (email == null || email.length() == 0 || email.indexOf("@") == -1) {
            return false;
        }
        int emailLength = email.length();
        int atPosition = email.indexOf("@");
        int atDot = reverse.indexOf(".");
    
        String beforeAt = email.substring(0, atPosition);
        String afterAt = email.substring(atPosition + 1, emailLength);
    
        if (beforeAt.length() == 0 || afterAt.length() == 0) {
            return false;
        }
        for (int i = 0; email.length() - 1 > i; i++) {
            char i1 = email.charAt(i);
            char i2 = email.charAt(i + 1);
            if (i1 == '.' && i2 == '.') {
                return false;
            }
        }
        if (email.charAt(atPosition - 1) == '.' || email.charAt(0) == '.' || email.charAt(atPosition + 1) == '.' || afterAt.indexOf("@") != -1 || atDot < 2) {
            return false;
        }
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 2015-10-21
      • 2010-11-24
      相关资源
      最近更新 更多