diff --git a/Address Validator/AddressValidator.py b/Address Validator/AddressValidator.py index 0dc49be1..9d0b8b3e 100644 --- a/Address Validator/AddressValidator.py +++ b/Address Validator/AddressValidator.py @@ -1,16 +1,83 @@ -def addressVal(address): - dot = address.find(".") - at = address.find("@") - if (dot == -1): - print("Invalid") - elif (at == -1): - print("Invalid") - else: - print("Valid") +import re +import dns.resolver +import idna + +# Regular expression for basic email syntax validation +email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' + +def is_valid_email(email): + """ + Validates an email address through multiple checks. + + Args: + email (str): The email address to validate. + + Returns: + bool: True if the email is valid, False otherwise. + """ + # Check if the email contains '@' + if '@' not in email: + return False + + # Split the email into local and domain parts + try: + local_part, domain_part = email.split('@') + except ValueError: + return False + + # Check length constraints for local and domain parts + if len(local_part) > 64 or len(domain_part) > 255 or len(email) > 320: + return False + + # Perform basic syntax validation + if not re.match(email_regex, email): + return False -print("This program will decide if your input is a valid email address") -while(True): - print("A valid email address needs an '@' symbol and a '.'") - x = input("Input your email address:") + # Validate the domain using DNS MX records + if not is_valid_domain(domain_part): + return False - addressVal(x) + # Validate if the domain supports internationalized domain names (IDNs) + if not validate_international_email(domain_part): + return False + + return True + +def is_valid_domain(domain): + """ + Checks if the domain has valid MX records. + + Args: + domain (str): The domain to check. + + Returns: + bool: True if the domain has valid MX records, False otherwise. + """ + try: + mx_records = dns.resolver.resolve(domain, 'MX') + return len(mx_records) > 0 + except Exception: + return False + +def validate_international_email(domain): + """ + Validates that the domain supports internationalized domain names (IDNs). + + Args: + domain (str): The domain to validate. + + Returns: + bool: True if the domain supports IDNs, False otherwise. + """ + try: + idna.encode(domain).decode('utf-8') + return True + except idna.IDNAError: + return False + +if __name__ == "__main__": + email_input = input("Enter your email address: ") + if is_valid_email(email_input): + print("The email address is valid.") + else: + print("The email address is invalid.") diff --git a/Address Validator/README.md b/Address Validator/README.md index 49dd8006..9661ae69 100644 --- a/Address Validator/README.md +++ b/Address Validator/README.md @@ -1,21 +1,25 @@ -![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99) +![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=flat&color=BC4E99) ![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103) # Address Validator +## 🛠️ Description +This program validates email addresses through multiple checks, including syntax validation, and DNS MX record verification. It ensures that the email address contains an '@' symbol, checks for proper length constraints, and verifies that the domain is not unauthorized email service. The program also supports internationalized domain names, enhancing its usability for a global audience. -## 🛠️ Description +## ⚙️ Languages and Packages Used -This program checks if your email adress is valid by looking for an '@' symbol and a '.' +- **Language**: Python +- **Packages**: + - `dnspython` - For DNS resolution and MX record checks + - `idna` - To handle internationalized domain names -## ⚙️ Languages or Frameworks Used -Python +## 🌟 How to Run -## 🌟 How to run -Open the file AddressValidator.py file with the python IDE and hit run. +Open the `AddressValidator.py` file with a Python IDE and hit run. +## 🤖 Authors -## 🤖 Author -tommcgurn10 +- **m-hassanqureshi** - Primary author of the renewed code +- **tommcgurn10** - Original author of the initial project diff --git a/requirements.txt b/requirements.txt index 9539deb4..6bed75d1 100644 Binary files a/requirements.txt and b/requirements.txt differ