Home | Русский

J2ME e-mail validator

Simple email validator in j2me.

	public static boolean validate(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) {
            System.out.println("only @ is");
            return false;
        }

        //CHECK for .(dot) before @(at) = aaa.@domain.com
        if (email.charAt(atPosition - 1) == '.') {
            System.out.println(".(dot) before @(at)");
            return false;
        }

        //CHECK for .(dot) before @(at) = aaa@.domain.com
        if (email.charAt(atPosition + 1) == '.') {
            System.out.println("@.");
            return false;
        }

        //CHECK for .(dot) = email@domaincom
        if (afterAt.indexOf(".") == -1) {
            System.out.println("no dot(.)");
            return false;
        }

        //CHECK for ..(2 dots) = email@domain..com
        char dotCh = 0;
        for (int i = 0; i < afterAt.length(); i++) {
            char ch = afterAt.charAt(i);
            if((ch == 0x2e) && (ch == dotCh)){
                System.out.println("find .. (2 dots) in @>");
                return false;
            }
            dotCh = ch;
        }

        //CHECK for double '@' = example@@domain.com
        if (afterAt.indexOf("@") != -1) {
            System.out.println("find 2 @");
            return false;
        }
        //check domain name 2-5 chars
        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 {
                    System.out.println("prefix not 2-5 chars");
                    return false;
                }
            } else {
                ind = newInd;
            }
        } while (true);

        //CHECK for valid chars[a-z][A-Z][0-9][. - _]
        //CHECK for ..(2 dots)
        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))) {
                System.out.println("not valid chars");
                return false;
            }
            if((ch == 0x2e) && (ch == dotCh)){
                System.out.println("find .. (2 dots)");
                return false;
            }
            dotCh = ch;
        }
        return true;
    }
This entry was written by admin , posted on Thursday June 25 2009at 04:06 am , filed under J2ME and tagged , , , . Bookmark the permalink . Post a comment below or leave a trackback: Trackback URL.

Leave a Reply

You must be logged in to post a comment.