In this post, you will see how you can design your own signup and login form in Python with user validation and start building your own apps. Afterwards, I have also included the email validation step. The password encryption step is not included, but you can rely on a third-party library.
Prerequisites:
- Familiarity with Python functions.
- Familiarity with Regex.
- Understanding of user input validation.
- Understanding of error validation.
What You Will Learn:
- How to create a signup form in Python
- How to create a login form in Python
- How to use regex for email validation
- A database to manage users and allow users to manage their data – account deletion.
- Age validation
So, let’s get started.
First of all, we are going to import the regex for email validation and create a database. Here I am using a simple dictionary but you can use Pandas or other database tools based on SQL as per the projects this is sufficient to show you how you can interact with the database.
import re
#create a database for users
users = {}
Code language: Python (python)
Next, we are going to write a function to allow users to sign up:
# create a signup form
def signup():
details = {}
# asking for name
name = input("Enter your name")
details["Name"] = name
# asking for age and verifying
try:
age = int(input("Enter your age. You must be at least 13."))
except ValueError:
print("Only numbers are allowed")
else:
if age < 13:
print("You must be at least 13")
else:
details["Age"] = age
# asking for email and verifying
email = input("Enter your email.")
# Create a Regex for Email Addresses
emailRegex = re.compile(r'''(
[a-zA-Z0-9._%+-1]+ #username
@ # @symbol
[a-zA-Z0-9.-]+ #domain name
(\.[a-zA-Z]{2,4}) #dot-something
)''', re.VERBOSE)
e = []
f = emailRegex.search(email)
if f == "None":
print("Enter an email")
else:
try:
e.append(f.group())
if e[0] in users:
print("This email is already taken")
else:
details["Email"] = e[0]
except AttributeError:
print("One email address is needed!")
# enter address
address = input("Enter your address")
details["Address"] = address
# enter username
username = input("Enter your username")
if username in users:
username = input("Enter a different username")
details["Username"] = username
else:
details["Username"] = username
# enter password -- You can also encrypt and decrypt the password using third party libarary
password = input("Enter your password")
details["Password"] = password
# updating our database
users.update({username : details})
print("\n")
print("Your Details Are:")
print(details)
Code language: Python (python)
Next, we are going to write a code to allow users to log in:
# to allow users to login
def login():
username = input("Enter your username")
if username not in users:
print("You don't have an account. Please create one")
else:
password = input("Enter your password")
if password != users[username]["Password"]:
print("Password did not match. Try again")
else:
print("You are successfully logged in") # you can input the process you want to start here.
Code language: Python (python)
Finally, a code to allow users to delete or remove their data:
# A function to allow users to deleted their accounts
def delete():
username = input("Enter your username")
if username not in users:
username = input("Enter your username")
else:
users.pop(username)
print("Account Deleted")
Code language: Python (python)
That’s it. Hope this was helpful. Feel free to drop your questions below. You must keep in mind password encryption if you creating a real-world project. Now if you want to move to a new project then see how you can create an e-commerce app in Python here.
Please note: the code above may not organize well on mobile devices so please use a laptop and code along to learn faster.