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

# -------------------------
# Subprograms
# -------------------------
---
def validate(email_address):
---
    # Validate prefix
    valid = True
    index = 0
    last_character = ""
---
    at_position = email_address.find("@")
---
    # Invalid if no @ symbol
    if at_position == -1:
---
        return False
---
    # Check each character is valid and stop once address is invalid
    while valid and index < at_position:
---
        # Digits are acceptable
        if email_address[index].isdigit():
---
            valid = True
---
        # Alphabet characters are acceptable
        elif email_address[index].isalpha():
---
            valid = True
---
        # Underscore, period or hyphen is acceptable but not two in a row
        elif email_address[index] in "_.-" and last_character not in "_.-":
---
            valid = True
---
        # If conditions are not met it is an invalid email address
        else:
---
            return False
---
        last_character = email_address[index]
---
        index = index + 1
---
    
    # Validate domain
    # Domain must be at least three characters
    if len(email_address) - at_position - 1 < 3:
---
        return False
---
    index = at_position + 1
---
    # Check each character is valid and stop once address is invalid
    while valid and index < len(email_address):
---
        # Digits are acceptable
        if email_address[index].isdigit():
---
            valid = True
---
        # Alphabet characters are acceptable
        elif email_address[index].isalpha():
---
            valid = True
---
        # Hyphens and periods are acceptable
        elif email_address[index] in "-.":
---
            valid = True
---
        # If conditions are not met it is an invalid email address
        else:
---
            return False
---
        index = index + 1
---
    return True
---
        
       
# -------------------------
# Main program
# -------------------------
---
email_address = input("Enter your email address: ")
---
valid = validate(email_address)
---
if valid:
---
    print("Email address is valid.")
---
else:
---
    print("Email address is invalid.")