coding
coding
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.
import re
"""
"""
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
"""
"""
phone_regex = r'^\d{10}$'
if re.match(phone_regex, phone):
return True
return False
"""
"""
zip_regex = r'^\d{5}$'
if re.match(zip_regex, zip_code):
return True
return False
if __name__ == "__main__":
email = "[email protected]"
phone = "1234567890"
zip_code = "94103"
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.
You can import this module into other Python scripts for reusability.
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.