AI-Assisted Coding Lab · Validate a Signup Form

30:00left of 30:00

Validate a Signup Form

The signup page needs server-side validation for its three fields. The README has the rules, and signup.py already checks username length and the password's letters and digits. Build the rest with the assistant: accept, edit, or reject each suggestion, and keep the tests green.

signup.py1 of 3

Files

signup.py

Python · Editable
import re
USERNAME_MIN = 3
USERNAME_MAX = 20
PASSWORD_MIN = 8
def validate_signup(username, email, password):
"""Returns {field: message} for each invalid field. An empty dict means the form is valid."""
username = username.strip()
email = email.strip()
problems = {
"username": username_error(username),
"email": email_error(email),
"password": password_error(password, username),
}
return {field: message for field, message in problems.items() if message}
def username_error(username):
"""Returns why the username is invalid, or None."""
if not USERNAME_MIN <= len(username) <= USERNAME_MAX:
return f"must be {USERNAME_MIN} to {USERNAME_MAX} characters"
return None
def email_error(email):
"""Returns why the email is invalid, or None."""
return None
def password_error(password, username):
"""Returns why the password is invalid, or None."""
if not any(c.isalpha() for c in password) or not any(c.isdigit() for c in password):
return "must include a letter and a digit"
return None

Test Output

No runs yet.

Assistant

Scripted
  1. Send the first prompt to start.

Next Prompt

Usernames should allow only letters, digits, and underscores, and start with a letter.