Is there email validator code for Java ME or BlackBerry?

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

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

3条回答
  •  迷失自我
    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;
        }
    

提交回复
热议问题