There are a lot of different ways to validate an email address input field. The easiest – and mostly correct – method is to use a regular expression. Two of the more complicated – and among the most incorrect – methods follow.

Jonasfound this in a custom-built web app at a customer site, and commented that it's emblematic of the quality of the whole codebase.

    private static boolean lastEmailFieldTwoCharsOrMore(String emailAddress) {
        if (emailAddress == null)
            return false;
        StringTokenizer st = new StringTokenizer(emailAddress, ".");
        String lastToken = null;
        while (st.hasMoreTokens()) {
            lastToken = st.nextToken();
        }

        if (lastToken.length() >= 2) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isValidEmailAddress(String emailAddress) {

        // a null string is invalid
        if (emailAddress == null)
            return false;

        // a string without a "@" is an invalid email address
        if (emailAddress.indexOf("@") < 0)
            return false;

        // a string without a "."  is an invalid email address
        if (emailAddress.indexOf(".") < 0)
            return false;

        if (lastEmailFieldTwoCharsOrMore(emailAddress) == false)
            return false;

        if (emailAddress.indexOf("!") > 0)
            return false;

        if (emailAddress.indexOf("#") > 0)
            return false;

        if (emailAddress.indexOf("$") > 0)
            return false;

        if (emailAddress.indexOf("%") > 0)
            return false;

        if (emailAddress.indexOf("&") > 0)
            return false;

        if (emailAddress.indexOf("*") > 0)
            return false;

        if (emailAddress.indexOf("+") > 0)
            return false;

        if (emailAddress.indexOf("-") > 0)
            return false;

        if (emailAddress.indexOf("~") > 0)
            return false;

        if (emailAddress.indexOf("ä") > 0)
            return false;

        if (emailAddress.indexOf("ö") > 0)
            return false;

        if (emailAddress.indexOf("å") > 0)
            return false;

        if (emailAddress.indexOf(";") > 0)
            return false;

        return true;
    }

 

Dan found this attempt in his company's PHP-based application.

if(ereg("^([a]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([b]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([c]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([d]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([e]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([f]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([g]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([h]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([i]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([j]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([k]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([l]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([m]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([n]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([o]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([p]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([q]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([r]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([s]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([t]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([u]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([v]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([w]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([x]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([y]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
if(ereg("^([z]{2,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,5})$", $email))
	return "CHAIN";
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!