0
0
Ruby on Railsframework~10 mins

Format validation in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A/\A[^@\s]+@[^@\s]+\z/
B/[a-z]+/
C/\s+/
D/\d+/
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.
2fill in blank
medium

Complete 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'
A"is invalid"
B"must be unique"
C"is too short"
D"can't be blank"
Attempts:
3 left
💡 Hint
Common Mistakes
Using messages unrelated to format validation.
Forgetting to put the message in quotes.
3fill in blank
hard

Fix 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'
A/[a-z]+/
B/[a-zA-Z0-9]/
C/\w+/
D/\A[a-zA-Z0-9]+\z/
Attempts:
3 left
💡 Hint
Common Mistakes
Using regex without anchors, allowing invalid characters.
Using \w+ which includes underscores.
4fill in blank
hard

Fill 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'
A/\A\d{10}\z/
B"must be 10 digits"
C"invalid phone number"
D/\d+/
Attempts:
3 left
💡 Hint
Common Mistakes
Using a regex that matches any number of digits.
Using a vague or unrelated error message.
5fill in blank
hard

Fill 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'
A/\A(?=.*[a-zA-Z])(?=.*\d).{8,}\z/
B"must include letters and numbers and be at least 8 characters"
Cfalse
Dtrue
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.