0% found this document useful (0 votes)
0 views

coding

Uploaded by

dia.batra0704
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

coding

Uploaded by

dia.batra0704
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Project Overview: Data Validation Module

The module will contain functions to validate user input, such as checking if an email is properly
formatted or if a given string is a valid phone number.

Python Code: data_validation.py

Here is the code:

import re

def is_valid_email(email: str) -> bool:

"""

Validates if the given string is a valid email address.

:param email: The email string to be validated

:return: True if valid, False otherwise

"""

email_regex = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'

if re.match(email_regex, email):

return True

return False

def is_valid_phone(phone: str) -> bool:

"""

Validates if the given string is a valid phone number.

A valid phone number is assumed to be 10 digits long for simplicity.

:param phone: The phone number string to be validated

:return: True if valid, False otherwise

"""

phone_regex = r'^\d{10}$'

if re.match(phone_regex, phone):
return True

return False

def is_valid_zip(zip_code: str) -> bool:

"""

Validates if the given string is a valid zip code.

A valid zip code is assumed to be 5 digits long.

:param zip_code: The zip code string to be validated

:return: True if valid, False otherwise

"""

zip_regex = r'^\d{5}$'

if re.match(zip_regex, zip_code):

return True

return False

if __name__ == "__main__":

# Test the functions

email = "[email protected]"

phone = "1234567890"

zip_code = "94103"

print(f"Is '{email}' a valid email? {is_valid_email(email)}")

print(f"Is '{phone}' a valid phone number? {is_valid_phone(phone)}")

print(f"Is '{zip_code}' a valid zip code? {is_valid_zip(zip_code)}")

Explanation of the Code:

is_valid_email: Uses a regular expression (regex) to check if the input string matches the pattern of a
valid email address (basic format).

is_valid_phone: Uses a regex to validate if the phone number is 10 digits long. You could extend this
to support international formats if needed.
is_valid_zip: Checks if the zip code is exactly 5 digits long (standard format for many countries like
the U.S.).

Testing: At the end of the script, a few test cases are included to demonstrate how these validation
functions work.

How to Use This Module:

You can import this module into other Python scripts for reusability.

from data_validation import is_valid_email, is_valid_phone, is_valid_zip

# Example of using the validation functions

if is_valid_email("[email protected]"):

print("Valid email")

else:

print("Invalid email")

This module could be expanded or integrated into a larger system where user input needs
validation, such as user registration or form submissions.

Output of the code

You might also like