Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to validate that the email attribute has the correct format.
Ruby on Rails
validates :email, format: { with: [1] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a regex that matches only letters or digits.
Not anchoring the regex to start and end of string.
✗ Incorrect
The regex /\A[^@\s]+@[^@\s]+\z/ ensures the email has a basic valid format with one '@' and no spaces.
2fill in blank
mediumComplete the code to add a custom error message for invalid email format.
Ruby on Rails
validates :email, format: { with: /\A[^@\s]+@[^@\s]+\z/, message: [1] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using messages unrelated to format validation.
Forgetting to put the message in quotes.
✗ Incorrect
The message "is invalid" clearly tells the user the email format is wrong.
3fill in blank
hardFix the error in the validation code to correctly validate a username with only letters and numbers.
Ruby on Rails
validates :username, format: { with: [1] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using regex without anchors, allowing invalid characters.
Using \w+ which includes underscores.
✗ Incorrect
The regex /\A[a-zA-Z0-9]+\z/ ensures the username contains only letters and numbers from start to end.
4fill in blank
hardFill both blanks to validate a phone number that must be exactly 10 digits.
Ruby on Rails
validates :phone, format: { with: [1], message: [2] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a regex that matches any number of digits.
Using a vague or unrelated error message.
✗ Incorrect
The regex /\A\d{10}\z/ matches exactly 10 digits, and the message clearly states the requirement.
5fill in blank
hardFill all three blanks to validate a password with at least 8 characters, including letters and numbers.
Ruby on Rails
validates :password, format: { with: [1], message: [2], allow_blank: [3] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a regex that does not check for both letters and digits.
Setting allow_blank to true, allowing empty passwords.
✗ Incorrect
The regex ensures at least one letter and one digit with minimum length 8. The message explains the rule. allow_blank: false means password cannot be empty.