Skip to content

About

Python email validator: checks length, format, @ symbol, domain, dots and invalid characters.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

Β 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Email Validator in Python

A clean, command-line email validator written in pure Python. It checks an email address against a clear set of rules and tells you exactly why an address is invalid. Includes a full unit-test suite.

Python Type Dependencies Tests


✨ Features

  • βœ… Validates emails typed in the terminal, again and again until you quit
  • πŸ’¬ Gives a specific, readable error message for every failed rule
  • 🧩 Reusable function validate_email() that you can import into other projects
  • πŸ”€ Supports real-world formats like name+tag@gmail.com and user@my-site.com
  • πŸ§ͺ Ships with unit tests covering valid emails, invalid emails and error messages
  • πŸ“¦ Pure Python: no libraries, no installation

πŸš€ Getting Started

Prerequisites

Run it

git clone https://github.com/<your-username>/email-validator-python.git
cd email-validator-python
python email_validator.py

Example session

========================================
        EMAIL VALIDATOR
========================================
Type an email to check it.
Type 'q' to quit.

Enter your email: palak_dev@gmail.com
βœ… Valid Email

Enter your email: palak@gmailcom
❌ Invalid Email: domain must contain a dot, like 'example.com'

Enter your email: q
Goodbye!

Use it in your own code

from email_validator import validate_email

is_valid, message = validate_email("palak@gmail.com")
print(is_valid, message)    # True Valid Email

validate_email() always returns a tuple: (True or False, message).


πŸ“‹ Validation Rules

Rules run in this order and stop at the first failure.

General checks

# Rule Error message
1 Not empty (spaces are trimmed first) Email cannot be empty
2 At least 6 characters Email is too short
3 At most 254 characters Email is too long (max 254 characters)
4 No spaces anywhere Email cannot contain spaces
5 Exactly one @ Email must contain exactly one '@'

Part before @ (local part)

# Rule Error message
6 Not empty Invalid Email: nothing before '@'
7 At most 64 characters Invalid Email: part before '@' is too long (max 64 characters)
8 Starts with a letter or digit Invalid Email: must start with a letter or a digit
9 Does not end with a dot Invalid Email: part before '@' cannot end with a dot
10 No consecutive dots (..) Invalid Email: consecutive dots are not allowed
11 Only letters, digits and . _ + - Invalid Email: invalid character '#' (allowed: letters, digits, . _ + -)

Part after @ (domain)

# Rule Error message
12 Not empty Invalid Email: nothing after '@'
13 Contains at least one dot Invalid Email: domain must contain a dot, like 'example.com'
14 No consecutive dots Invalid Email: consecutive dots are not allowed
15 Does not start or end with a dot Invalid Email: domain cannot start or end with a dot
16 Each part is at most 63 characters Invalid Email: domain part '...' is too long
17 Parts do not start or end with a hyphen Invalid Email: domain parts cannot start or end with a hyphen
18 Parts contain only letters, digits and - Invalid Email: invalid character '_' in domain (allowed: letters, digits, -)
19 Extension (last part) is 2+ letters Invalid Email: domain extension must be at least 2 letters (like .com or .in)

If every rule passes, the result is Valid Email.


πŸ§ͺ Example Results

Input Result Reason
palak_dev@gmail.com βœ… Valid Passes all rules
john.doe@example.co.in βœ… Valid Multiple dots in the domain are fine
name+tag@gmail.com βœ… Valid + is allowed
user@my-site.com βœ… Valid Hyphen inside a domain part is allowed
1palak@gmail.com βœ… Valid A digit may start the email
ab@c ❌ Too short
palakgmail.com ❌ No @
palak@@gmail.com ❌ Two @ symbols
palak dev@gmail.com ❌ Contains a space
_palak@gmail.com ❌ Must start with a letter or digit
pa..lak@gmail.com ❌ Consecutive dots
palak.@gmail.com ❌ Local part ends with a dot
palak#1@gmail.com ❌ # is not allowed
palak@gmailcom ❌ Domain has no dot
palak@.com ❌ Domain starts with a dot
palak@-gmail.com ❌ Domain part starts with a hyphen
palak@gmail.c ❌ Extension is shorter than 2 letters

πŸ”„ How It Works

flowchart TD
    A([Start]) --> B[/"Read email and trim spaces"/]
    B --> C{"General checks pass? (not empty, length, no spaces, one at-sign)"}
    C -- No --> X1["Return False and message"]
    C -- Yes --> D["Split into local part and domain"]
    D --> E{"Local part valid?"}
    E -- No --> X2["Return False and message"]
    E -- Yes --> F{"Domain valid?"}
    F -- No --> X3["Return False and message"]
    F -- Yes --> G(["Return True and Valid Email"])
Loading

Design

Function Job
check_local_part(local) Validates the part before @. Returns an error message or None
check_domain(domain) Validates the part after @. Returns an error message or None
validate_email(email) Runs all checks. Returns (bool, message)
main() Command-line loop: asks for emails until you type q

Each helper returns None when everything is fine and a message when something is wrong, which keeps validate_email() short and easy to read.


πŸ§ͺ Running the Tests

python -m unittest -v

The test suite checks:

  • 7 valid emails (including +, hyphens and surrounding spaces)
  • 24 invalid emails (each rule is covered)
  • the exact wording of key error messages

🧠 Concepts Practiced

  • Functions with type hints and tuple returns
  • Constants and set operations (LETTERS | DIGITS)
  • Early-return pattern for clean validation logic
  • String methods: strip(), split(), count(), startswith(), endswith()
  • f-strings for helpful error messages
  • while True loops and if __name__ == "__main__"
  • Unit testing with unittest and subTest

⚠️ Limitations

This validator is practical but not a full implementation of the email standard (RFC 5322).

Limitation Example
Does not check that the mailbox or domain really exists abcdef@notarealdomain.com passes
Non-English (Unicode) addresses are rejected Γ©lan@example.com fails
Rare symbols allowed by the standard are rejected user!tag@example.com fails
Quoted names and IP-address domains are not supported "john doe"@example.com fails

For production use, rely on a well-tested library such as email-validator and send a confirmation email to prove the address works.


πŸ›£οΈ Ideas for Improvement

  • Add a regular expression version and compare it with this one
  • Validate a list of emails from a text file and write a report
  • Accept the email as a command-line argument using argparse
  • Add an optional DNS check to see whether the domain exists
  • Build a small web form (Flask or React + API) around validate_email()
  • Package it and publish it to PyPI

πŸ“ Project Structure

email-validator-python/
β”œβ”€β”€ email_validator.py         # validation logic + command-line app
β”œβ”€β”€ test_email_validator.py    # unit tests
└── README.md                  # project documentation

🀝 Contributing

Suggestions and improvements are welcome. Feel free to open an issue or submit a pull request.


πŸ“„ License

This project is open source and available under the MIT License.


⭐ If this helped you learn something, consider giving the repo a star!

About

Python email validator: checks length, format, @ symbol, domain, dots and invalid characters.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages