Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to validate email address
Email validation is a common requirement in web applications. We need to check whether an email address follows the correct format and character rules. Python's re module provides powerful regular expression capabilities for this task.
Email Validation Rules
A valid email must follow these conditions ?
Format must be username@company.domain
Username can contain letters, numbers, dashes, and underscores
Company name can contain letters and numbers only
Domain can contain lowercase letters only
Domain extension length must be 1-3 characters
Using Regular Expression Pattern
We create a regex pattern that matches all validation rules ?
import re
def validate_email(email):
# Pattern breakdown:
# ^[a-zA-Z0-9-_]+ - Username: letters, numbers, dash, underscore
# @ - Literal @ symbol
# [a-zA-Z0-9]+ - Company: letters and numbers
# \. - Literal dot (escaped)
# [a-z]{1,3}$ - Domain: 1-3 lowercase letters at end
pattern = "^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\.[a-z]{1,3}$"
if re.match(pattern, email):
return True
return False
# Test with valid email
email1 = "popular_website15@company123.com"
print(f"'{email1}' is valid: {validate_email(email1)}")
# Test with invalid emails
email2 = "invalid@company.DOMAIN" # Domain not lowercase
email3 = "user@.com" # No company name
email4 = "user@company.toolong" # Domain too long
print(f"'{email2}' is valid: {validate_email(email2)}")
print(f"'{email3}' is valid: {validate_email(email3)}")
print(f"'{email4}' is valid: {validate_email(email4)}")
'popular_website15@company123.com' is valid: True 'invalid@company.DOMAIN' is valid: False 'user@.com' is valid: False 'user@company.toolong' is valid: False
Alternative Approach with Detailed Validation
For more detailed error reporting, we can validate each part separately ?
import re
def detailed_email_validation(email):
# Split email into parts
if '@' not in email or email.count('@') != 1:
return False, "Must contain exactly one @ symbol"
username, domain_part = email.split('@')
if '.' not in domain_part or domain_part.count('.') != 1:
return False, "Domain must contain exactly one dot"
company, domain = domain_part.split('.')
# Validate username
if not re.match("^[a-zA-Z0-9-_]+$", username):
return False, "Username contains invalid characters"
# Validate company
if not re.match("^[a-zA-Z0-9]+$", company):
return False, "Company name contains invalid characters"
# Validate domain
if not re.match("^[a-z]{1,3}$", domain):
return False, "Domain must be 1-3 lowercase letters"
return True, "Valid email"
# Test different emails
test_emails = [
"user123@company.com",
"user@company.COM",
"user$@company.com",
"user@company123.co"
]
for email in test_emails:
is_valid, message = detailed_email_validation(email)
print(f"'{email}': {message}")
'user123@company.com': Valid email 'user@company.COM': Domain must be 1-3 lowercase letters 'user$@company.com': Username contains invalid characters 'user@company123.co': Valid email
Pattern Explanation
| Pattern Part | Meaning | Example Match |
|---|---|---|
^[a-zA-Z0-9-_]+ |
Username: letters, numbers, dash, underscore | user_123 |
@ |
Literal @ symbol | @ |
[a-zA-Z0-9]+ |
Company: letters and numbers | company123 |
\. |
Literal dot (escaped) | . |
[a-z]{1,3}$ |
Domain: 1-3 lowercase letters at end | com |
Conclusion
Regular expressions provide an efficient way to validate email formats. Use re.match() with a proper pattern to check all validation rules in one step. For complex validation scenarios, consider breaking down the validation into separate checks for better error reporting.
