Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Validation of an email address.

★★★

A valid email address consists of an email prefix and an email domain, both in acceptable formats. The prefix appears to the left of the @ symbol. The domain appears to the right of the @ symbol. For example, in the address [email protected], "example" is the email prefix, and "mail.com" is the email domain. Basic validation of an address is carried out client-side before data is sent to be processed server-side.

Valid address

Make

Write a program that validates an email address.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Acceptable email prefix formats:

Acceptable email domain formats:

Complete the subprogram called validate that:

  1. Returns true if the email_address is valid or false if not.

Complete the main program so that:

  1. The user can input the email address to check.

Typical inputs and outputs from the program would be:

Enter your email address: 
[email protected]
Email address is invalid.
Enter your email address: 
[email protected]
Email address is valid.
Enter your email address: 
[email protected]
Email address is invalid.
Enter your email address: 
[email protected]
Email address is valid.
Enter your email address: 
[email protected]@
Email address is invalid.
Enter your email address: 
[email protected]
Email address is valid.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Valid address program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    static bool validate(string email_address)
    {
---
        // Validate prefix
        bool valid = true;
        int index = 0;
        char last_character = ' ';
        int at_position = email_address.IndexOf("@");
---
        // Invalid if no @ symbol
        if (at_position == -1)
        {
            return false;
        }
---
        // Check each character is valid and stop once address is invalid
        while (valid && index < at_position)
        {
---
            // Digits are acceptable
            if (char.IsDigit(email_address[index]))
            {
                valid = true;
            }
---
            // Alphabet characters are acceptable
            else if (char.IsLetter(email_address[index]))
            {
                valid = true;
            }
---
            // Underscore, period or hyphen is acceptable but not two in a row
            else if ("_.-".Contains(email_address[index]) && !"_.-".Contains(last_character))
            {
                valid = true;
            }
---
            // If conditions are not met it is an invalid email address
            else
            {
                return false;
            }
---
            last_character = email_address[index];
            index++;
        }
---
        // A separator may not be the last character before the @ symbol
        if ("_.-".Contains(last_character))
        {
            return false;
        }
---
        // Validate domain
        // Domain must be at least three characters
        if (email_address.Length - at_position - 1 < 3)
        {
            return false;
        }
        index = at_position + 1;
---
        // Check domain
        while (valid && index < email_address.Length)
        {
---
            // Digits are acceptable
            if (char.IsDigit(email_address[index]))
            {
                valid = true;
            }
---
            // Alphabet characters are acceptable
            else if (char.IsLetter(email_address[index]))
            {
                valid = true;
            }
---
            // Hyphens and periods are acceptable
            else if ("-.".Contains(email_address[index]))
            {
                valid = true;
            }
---
            else
            {
                return false;
            }
---
            index++;
        }
        return valid;
    }


---
    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter your email address: ");
        string email_address = Console.ReadLine();
        bool valid = validate(email_address);
---
        if (valid)
        {
            Console.WriteLine("Email address is valid.");
        }
        else
        {
            Console.WriteLine("Email address is invalid.");
        }
---
    }
}