Is there email validator code for Java ME or BlackBerry?

后端 未结 3 1726
半阙折子戏
半阙折子戏 2021-01-17 02:43

Is there some standard email validator code sample for Java ME or BlackBerry?

相关标签:
3条回答
  • 2021-01-17 03:15

    You can simply google around for email validation regex pattern. Thats the easiest and efficient way to check email string format. see the link below.

    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/

    You will find many other examples for regex. For Regex, check the following link.

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

    0 讨论(0)
  • 2021-01-17 03:21

    Use this code for checking the given email id is valid or not,

    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;
        }
    
    0 讨论(0)
  • 2021-01-17 03:28
    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;
    }
    
    0 讨论(0)
提交回复
热议问题