single
Challenge: Implement a Validation Flow
Pyyhkäise näyttääksesi valikon
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".
Ratkaisu
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".
Ratkaisu
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.
Kiitos palautteestasi!
single
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme