Ejemplo n.º 1
0
        public static string CheckEmailValidity(ref string input)
        {
            if (input == null)
            {
                return("No email found as input");
            }
            input = input.ToLower();
            if (input.Length < 10)
            {
                return("Too short");
            }
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == ' ')
                {
                    return("Email contains white space");
                }
                if (!((input[i] >= 'a' && input[i] <= 'z') || input[i] == '_' || input[i] == '.' || input[i] == '@' || (input[i] >= '0' && input[i] <= '9')))
                {
                    return("Contains invalid characters");
                }
            }
            int itr = 0;

            while (itr < input.Length && input[itr] != '@')
            {
                itr++;
            }
            if (itr >= input.Length)
            {
                return("Incomplete");
            }
            itr++;
            int domainPrefix = 0;

            while (itr < input.Length && ((input[itr] >= 'a' && input[itr] <= 'z') || input[itr] == '_' || (input[itr] >= '0' && input[itr] <= '9')))
            {
                domainPrefix++;
                itr++;
            }
            if (domainPrefix < 2)
            {
                return("Incomplete email domain");
            }
            if (itr >= input.Length)
            {
                return("Incomplete");
            }
            if (input[itr] != '.')
            {
                return("Domain contains invalid characters");
            }
            itr++;
            char last = '.';

            while (itr < input.Length)
            {
                if (!((input[itr] >= 'a' && input[itr] <= 'z') || (input[itr] >= '0' && input[itr] <= '9') || input[itr] == '.' || input[itr] == '_'))
                {
                    return("Domain contains invalid characters");
                }
                if (last == input[itr] && last == '.')
                {
                    return("Domain contains invalid characters");
                }
                last = input[itr];
                itr++;
            }
            if (!((input[input.Length - 1] >= 'a' && input[input.Length - 1] <= 'z') || (input[input.Length - 1] >= '0' && input[input.Length - 1] <= '9') || input[input.Length - 1] == '_'))
            {
                return("Domain contains invalid characters");
            }
            if (input.Length > 45)
            {
                return("Too long");
            }

            bool?existsOnDatabase = ServerRequest.EmailAlreadyExists(input);

            if (existsOnDatabase == true)
            {
                return("Email already belongs to a user");
            }
            else if (existsOnDatabase == null)
            {
                return("Availability checking failed");
            }

            return(null);
        }