开发者

Is there email validator code for Java ME or BlackBerry?

Is there some standard email validator开发者_如何学Go code sample for Java ME or BlackBerry?


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;
}


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;
    }


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜