Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Address_Validator_Improved/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 📧 Improved Email Validator

This project provides a structured email validation script written in Python.
It improves upon simple checks (like just searching for `@` and `.`) by applying multiple
rules to accurately determine whether an email address is correctly formatted.

---

## 🚀 Features

### ✔ 1. Validates correct ordering of `@` and `.`
- Ensures `.` appears **after** `@`.
- Prevents invalid formats like:
- `abc.gmail@com`
- `@gmail.com`
- `abc@gmail.`

### ✔ 2. Checks proper placement of characters
- Local part (before @) must not be empty.
- Domain must have a valid extension (like `.com`, `.in`, `.org`).

### ✔ 3. Handles spaces, dots, and case sensitivity
- Strips leading/trailing spaces.
- Converts email to lowercase.
- Detects consecutive dots such as `[email protected]`.

### ✔ 4. Provides detailed error messages
Instead of printing just “invalid”, the validator explains *why* the email is invalid.

### ✔ 5. Uses regex + rule-based logic
A hybrid approach ensures both flexibility and safety.

---

## 🧪 Example Output
### Input

[email protected]

### Output

✓ Email is valid!

### Input

@domain.com

### Output

✗ Email is invalid due to:
- Email must contain text before '@'.
- Email contains invalid characters or structure.

## How to Run

```bash
python email_validator.py
75 changes: 75 additions & 0 deletions Address_Validator_Improved/address_validator_improved.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import re

def validate_email(email: str) -> dict:

result = {
"original_email": email,
"is_valid": True,
"errors": []
}

email = email.strip().lower()

# Rule 1: Email must not be empty
if not email:
result["is_valid"] = False
result["errors"].append("Email cannot be empty.")
return result

# Rule 2: Must contain exactly one '@'
if email.count("@") != 1:
result["is_valid"] = False
result["errors"].append("Email must contain exactly one '@' symbol.")
return result

local_part, domain_part = email.split("@")

# Rule 3: Local part must not be empty
if not local_part:
result["is_valid"] = False
result["errors"].append("Email must contain text before '@'.")

# Rule 4: Domain part must contain at least one '.'
if "." not in domain_part:
result["is_valid"] = False
result["errors"].append("Email must contain at least one '.' after '@'.")
return result

# Rule 5: Domain extension must be present
domain_name, *extensions = domain_part.split(".")
if not domain_name:
result["is_valid"] = False
result["errors"].append("Domain name cannot start with a dot.")

if any(ext == "" for ext in extensions):
result["is_valid"] = False
result["errors"].append("Email cannot end with a dot or contain empty extensions.")

# Rule 6: Prevent consecutive dots
if ".." in email:
result["is_valid"] = False
result["errors"].append("Email cannot contain consecutive dots '..'.")

# Rule 7: Basic character whitelist
allowed_pattern = r"^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,}$"
if not re.match(allowed_pattern, email):
result["is_valid"] = False
result["errors"].append("Email contains invalid characters or structure.")

return result


if __name__ == "__main__":
print("------ Improved Email Validator ------")
user_email = input("Enter an email address: ")

response = validate_email(user_email)

if response["is_valid"]:
print("\n✓ Email is valid!")
else:
print("\n✗ Email is invalid due to:")
for err in response["errors"]:
print(f" - {err}")


Empty file.
Binary file not shown.