0

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?

manan
  • 1,385
  • 13
  • 23
Meenu Shankar
  • 63
  • 1
  • 10

3 Answers3

2

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

AdityaKeyal
  • 1,208
  • 8
  • 14
  • Your code seems broken, it doesn't allow '+' or '-'. – Fredrik Apr 25 '14 at 05:29
  • @Fredrik - I have not encountered email addresses normally with +- (I guess I am more of a old school email id person :) ) which is why I have put them in the exclusion list (refer - deniedIdCharList variable) If required we can remove it from the same. – AdityaKeyal Apr 25 '14 at 05:31
  • 2
    Trust me, I am quite old school when it comes to e-mail. Using + is a rather standardized way to split the part before the domain into a user and a topic to make it easier to filter things or simply not have to hand out the same address to everyone... Like john+lists@domain.com instead of john@domain.com. '-' is both valid but also something you see more and more often in domain names. You might not want to exclude the guys at experts-exchange.com from your services. – Fredrik Apr 25 '14 at 05:39
  • Even if you don't see it very often, it is good to stick to what is allowed. I once failed to order something from the UK because the guy who wrote the system counted the dots in the domain to validate the e-mail. In the UK, almost all domains ends with .co.uk so he assumed that if there were less than two dots, it was invalid... – Fredrik Apr 25 '14 at 05:41
  • ouch.. noted and agreed. will keep that in mind in future. also I have updated the code to allow + and -. Thanks for pointing it out. – AdityaKeyal Apr 25 '14 at 05:44
1

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
vikeng21
  • 543
  • 8
  • 28
  • Regexes (or any local method for that matter) for email validation is a spectacularly bad idea. You validate emails by sending things to them that require some action. And, in fact, you should do that periodically in case the email disappears. – paxdiablo Sep 24 '14 at 05:05
0

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.