single
Challenge: Implement a Validation Flow
Sveip for å vise menyen
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.
Takk for tilbakemeldingene dine!
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår