runtime
179 / 500
question 179 Exceptions & Validation · hard

Validate a sign-up form, every field at once

A sign-up form must show every problem at once, next to the field it belongs to. Write one checker per field that raises ValueError with the field's message, then validate_signup(form) that runs them all and collects the messages.

Functionvalidate_signup(form) returns {field: message} for every invalid field, in the order username, email, age; {} when all are valid
username3–20 characters, only letters, digits and _: message username must be 3-20 letters, digits or _
emailexactly one @, with at least one . after it and something before it: message email address is not valid
agea whole number from 13 to 120 (text from the form): message age must be a whole number, or age must be 13-120
Missinga field missing from form counts as the empty text ""
Examples
callvalidate_signup({'username': 'ana_dev', 'email': 'ana@runtime.school', 'age': '34'})returns{}
callvalidate_signup({'username': 'a!', 'email': 'ana.runtime.school', 'age': 'twelve'})returns{'username': 'username must be 3-20 letters, digits or _', 'email': 'email address is not valid', 'age': 'age must be a whole number'}
callvalidate_signup({'username': 'theo99', 'email': 'theo@mail.com', 'age': '130'})returns{'age': 'age must be 13-120'}

Loading your progress…

Validate a sign-up form, every field at once — Python practice · Runtime