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.
- β 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.comanduser@my-site.com - π§ͺ Ships with unit tests covering valid emails, invalid emails and error messages
- π¦ Pure Python: no libraries, no installation
- Python 3.10 or newer (download)
git clone https://github.com/<your-username>/email-validator-python.git
cd email-validator-python
python email_validator.py========================================
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!
from email_validator import validate_email
is_valid, message = validate_email("palak@gmail.com")
print(is_valid, message) # True Valid Emailvalidate_email() always returns a tuple: (True or False, message).
Rules run in this order and stop at the first failure.
| # | 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 '@' |
| # | 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, . _ + -) |
| # | 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.
| 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 |
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"])
| 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.
python -m unittest -vThe 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
- 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 Trueloops andif __name__ == "__main__"- Unit testing with
unittestandsubTest
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-validatorand send a confirmation email to prove the address works.
- 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
email-validator-python/
βββ email_validator.py # validation logic + command-line app
βββ test_email_validator.py # unit tests
βββ README.md # project documentation
Suggestions and improvements are welcome. Feel free to open an issue or submit a pull request.
This project is open source and available under the MIT License.
β If this helped you learn something, consider giving the repo a star!