Question

I am writing a java program on email validation.

The code must not contain:

  • built-in functions (aside from String methods and the like)
  • regular expressions

The code must contain:

  • loops
  • collections

I understand that email validation is harder without the use of regular expressions, but this question has been asked in an interview.

Are there any possibilities to write such a code or any alternate methods?

Was it helpful?

Solution

Use something like this :

public class EmailValidation {
    public static void main(String[] args) {
        String email = "SAMPLE_EMAIL";

        String[] deniedIdCharList = { ",", ";", "'", "\"", "[", "]", "|", "\\",
                 "=",  "!", "#", "$", "%", "^", "&", "*", "(", ")",
                "/", "`", "~", ":", "<", ">", "?", "{", "}" };

        int atLoc = email.indexOf("@");
        if (atLoc == -1) {
            System.out.println("fail");
        } else {
            String id = email.substring(0, atLoc);
            String domain = email.substring(atLoc + 1, email.length());

            if (domain.indexOf("@") != -1) {
                System.out.println("fail");
            }

            else {

                for (String deny : deniedIdCharList) {
                    if (id.indexOf(deny) != -1) {
                        System.out.println("fails");
                    }
                    if (domain.indexOf(deny) != -1) {
                        System.out.println("fails");
                    }

                }
                if (id.length() == 0 || domain.length() == 0) {
                    System.out.println("fails");
                }

                int dotIndex = domain.indexOf(".");
                String host = domain.substring(0, dotIndex);
                String extn = domain.substring(dotIndex + 1);
                if (host.length() == 0) {
                    System.out.println("fail");
                }
                if ((extn.length() != 2 && extn.length() != 3 && extn.length() != 5)) {
                    System.out.println("fail");
                }
                if (extn.length() == 5 && extn.indexOf(".") == -1) {
                    System.out.println("fail");
                }

            }

        }

    }
}

This worked for most standard checks I subjected it to. The code can be improved (A LOT) in terms of efficiency, however my guess is this is more from a "Can it be done" or Academic point of view rather than usage perspective. If you plan to use this methodology I advise strongly against it and refer to the answer provided by @vikeng21

OTHER TIPS

Using regular expressions is your best option if you have to do it locally (rather than validating through the use of an email). But if you don't want to use them, you will end with lot of clutter in your code managing it.

How to get over the problem. there are many ways

  1. Using the String Api to check for the @ symbol in the string.
  2. Using StringTokeniser to tokenize the given string use the condition checking.
  3. splitting the given string into multiple child strings and splitting them further.

and many more.

I would suggest not go through all this trouble of writing boiler plate code and say hi to regular expressions.

There can be many forms of email validation depending upon how your email address looks like. If it looks as simple as this as harry@potter.com then there can be many ways to validate this. One of it can be checking if '@' and '.' occurs only once in a for loop and that '.' always is followed after '@'.

If you give us some example of valid email addresses given as an example in the interview question, some of us can come up with a program to solve it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top