single
Challenge: Implement a Validation Flow
Stryg for at vise menuen
Swipe to start coding
Create a validation function for a user registration form that checks the username, email, and password fields. If a field is invalid, the function must throw a ValidationException with a relevant message.
- If
usernameis empty, throw aValidationExceptionwith the message "Username cannot be empty". - If
emailis not a valid email address, throw aValidationExceptionwith the message "Invalid email address". - If
passwordis less than 8 characters long, throw aValidationExceptionwith the message "Password must be at least 8 characters long". - If all fields are valid, return the string "Registration successful".
Løsning
Swipe to start coding
Create a validation function for a user registration form that checks the username, email, and password fields. If a field is invalid, the function must throw a ValidationException with a relevant message.
- If
usernameis empty, throw aValidationExceptionwith the message "Username cannot be empty". - If
emailis not a valid email address, throw aValidationExceptionwith the message "Invalid email address". - If
passwordis less than 8 characters long, throw aValidationExceptionwith the message "Password must be at least 8 characters long". - If all fields are valid, return the string "Registration successful".
Løsning
Create a validation function for a user registration form that checks the username, email, and password fields. If a field is invalid, the function must throw a ValidationException with a relevant message.
- If
usernameis empty, throw a ValidationException with the messageUsername cannot be empty; - If
emailis not a valid email address, throw a ValidationException with the messageInvalid email address; - If
passwordis less than 8 characters long, throw a ValidationException with the messagePassword must be at least 8 characters long; - If all fields are valid, return the string
Registration successful.
Unit Test Checklist
- Throws a
ValidationExceptionwith the correct message ifusernameis empty; - Throws a
ValidationExceptionwith the correct message ifemailis not a valid email address; - Throws a
ValidationExceptionwith the correct message ifpasswordis less than 8 characters long; - Returns "Registration successful" if all fields are valid.
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat