Skip to content
Project: Creating Functions to Register App Users
You are a junior developer working in a small start-up. Your managers have asked you to develop a new account registration system for a mobile app. The system must validate user input on the sign-up form before creating an account.
The previous junior developer wrote some helper functions that validate the name, email, and password. Use these functions to register users, store their data, and implement some error handling! These have been imported into the workspace for you. They will be a great help to you when registering the user, but first you have to understand what the function does! Inspect the docstrings of each of the helper functions: validate_name, validate_email and validate_password.
# Re-run this cell and examine the docstring of each function
from python_functions import validate_name, validate_email, validate_password, top_level_domains
print("validate_name\n")
print(validate_name.__doc__)
print("--------------------\n")
print("validate_email\n")
print(validate_email.__doc__)
print("--------------------\n")
print("validate_password\n")
print(validate_password.__doc__)
# The top level domains variable is used in validate_email to approve only certain email domains
print(top_level_domains)# Start coding here
# Validate name
def validate_name(name):
if type(name) != str:
return False
elif len(name) < 2:
return False
else:
return Truedef validate_email(email):
# Check if email contains '@'
if '@' not in email:
return False
# Split the email into username and domain parts
parts = email.split('@')
# Check if there are exactly 2 parts (username and domain)
if len(parts) != 2:
return False
username, domain = parts
# Check username length > 1 character
if len(username) <= 1:
return False
# Check if domain ends with any allowed top-level domain
for tld in top_level_domains:
if domain.endswith(tld):
return True
return False# Validate Password
def validate_password(password):
return (len(password) > 8 and
any(char.isupper() for char in password) and
any(char.isdigit() for char in password))def validate_user(name,email,password):
# Validate name
if not validate_name(name):
raise ValueError("Invalid name. Name must be a string.")
# Validate email
if not validate_email(email):
raise ValueError("Invalid email. Must contain '@' and a valid top-level domain.")
# Validate password
if not validate_password(password):
raise ValueError("Invalid password. Must be >8 characters, contain a capital letter and a digit.")
# All validations passed
return Truedef register_user(name, email, password):
try:
# Call validate_user to check all inputs
validate_user(name, email, password)
# If validation passes, create and return the user dictionary
user_data = {
"name": name,
"email": email,
"password": password
}
return user_data
except ValueError as e:
# Catch the ValueError raised by validate_user() and return False
return False validate_user('John', '[email protected]', 'securePassword123')