single
Challenge: Implement a Validation Flow
Svep för att visa menyn
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.
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal