Email Validator in Python.
About the project:
The Email Validator in Python project demonstrates how to verify whether an email address follows a valid format using regular expressions. Email validation is a common requirement in real-world applications such as login systems, signup forms, and user data collection.
This project uses Python’s built-in re module to match email patterns and provides instant feedback through a command-line interface.
What You Will Learn from This Project
- How email validation works at a basic level
- How to use regular expressions in Python
- How to validate user input safely
- How to write reusable validation functions
- How to build interactive command-line programs
How to use this program:
Run the Python script.
When you run it, you can enter any email address, and the program will tell you if it's valid.
The program will continue to run until you type q to quit.
The program will use regular expressions to perform a robust validation of the email format.
It will run in a continuous loop, so you can check multiple email addresses without restarting the program.
Project Level: Beginner
Concepts Covered: Regular expressions, string validation, loops, user input
How Email Validation Works
Email validation checks whether a given string follows the standard structure of an email address. While full validation requires server-side verification, format checking helps prevent incorrect input early.
A typical email format includes:
- A username (letters, numbers, dots, or symbols)
- An
@symbol - A domain name
- A valid top-level domain such as
.com,.org, or.net
Understanding the Regular Expression Used
The script uses the following regular expression to validate emails:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
^and$ensure the entire string is validated- The first part matches the email username
@separates the username and domain- The domain and extension are validated at the end
This pattern covers most common email formats used in real applications.
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# email_validator.py
import re
def validate_email(email):
"""
Validates an email address using a regular expression.
The regex checks for a basic email format:
- A username (one or more word characters, dots, or hyphens)
- Followed by an '@' symbol
- Followed by a domain name (one or more word characters, dots, or hyphens)
- Followed by a top-level domain (e.g., .com, .org, .net)
Args:
email (str): The email address to validate.
Returns:
bool: True if the email is in a valid format, False otherwise.
"""
# Regex for a basic email validation
regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
# Use re.fullmatch() to ensure the entire string matches the pattern
if re.fullmatch(regex, email):
return True
else:
return False
def main():
"""
Main function to run the Email Validator in a continuous loop.
"""
print("--- Python Email Validator ---")
print("Enter an email address to validate (or 'q' to quit).")
while True:
user_input = input("\nEnter an email address: ").strip()
# Check if the user wants to quit
if user_input.lower() == 'q':
print("Exiting Email Validator. Goodbye!")
break
if not user_input:
print("Please enter an email address.")
continue
if validate_email(user_input):
print(f"Result: '{user_input}' is a valid email address.")
else:
print(f"Result: '{user_input}' is not a valid email address.")
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()
Sample Output
--- Python Email Validator --- Enter an email address to validate (or 'q' to quit). Enter an email address: test@example.com Result: 'test@example.com' is a valid email address. Enter an email address: test@wrong Result: 'test@wrong' is not a valid email address.
Real-World Uses of Email Validation
- User registration and login systems
- Newsletter signup forms
- Contact forms and feedback systems
- Preventing invalid data entry in databases
- Input validation for APIs
How You Can Improve This Project
- Add domain verification using DNS checks
- Validate multiple emails from a file
- Integrate this validator into a web form
- Improve regex for stricter validation
- Log valid and invalid emails to a file
Conclusion
This Email Validator project introduces an essential real-world concept used in almost every application that collects user data. By combining Python’s regular expressions with clean input handling, you can build reliable validation tools.
Mastering input validation is a key step toward writing secure and professional Python applications.
Related Python tutorials you may find useful:
← Back to Projects
