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
# Use as many cells as you needimport re
top_level_domains = ['.org', '.net', '.edu', '.ac', '.uk', '.com']
def validate_name(name):
return isinstance(name, str) and len(name) > 2
def validate_email(email):
if isinstance(email, str) and '@' in email:
username, domain = email.split('@', 1)
domain_suffix = '.' + domain.rsplit('.', 1)[-1]
return len(username) > 1 and domain_suffix in top_level_domains
return False
def validate_password(password):
return (len(password) > 8 and re.search(r'[A-Z]', password) and re.search(r'[0-9]', password))
def validate_user(name, email, password):
if validate_name(name) == False:
raise ValueError('Name must be more than two characters and must be a string')
elif validate_email(email) == False:
raise ValueError('Invalid Email format, email must contain username, @ and domain name')
elif validate_password(password) == False:
raise ValueError('Password not strong enough. Ensure the password is more than 8 characters, contains numeric and has at least one capital letter.')
else:
return True
def register_user(name, email, password):
try:
validate_user(name, email, password)
except ValueError:
return False
else:
return {"name": name, "email": email, "password": password}