Why might this validation cause unexpected failures for typical emails?
medium
A. Because the regex only allows letters, blocking digits and symbols in emails
B. Because @Email does not allow uppercase letters
C. Because @Pattern requires the field to be empty
D. Because @Email and @Pattern cannot be used on the same field
Solution
Step 1: Analyze the regex pattern
The regex [a-zA-Z]+ allows only letters, no digits, dots, or @ symbols which are common in emails.
Step 2: Understand impact on email validation
This pattern blocks valid email characters like digits, dots, and '@', causing valid emails to fail validation.
Final Answer:
Because the regex only allows letters, blocking digits and symbols in emails -> Option A
Quick Check:
Regex must allow email characters [OK]
Hint: Regex must include all valid email characters [OK]
Common Mistakes:
Assuming @Email restricts uppercase letters
Thinking @Pattern requires empty field
Believing @Email and @Pattern conflict
5. You want to validate a Spring Boot entity field so it accepts only emails from mycompany.com domain. Which annotation setup is correct?
hard
A. @Pattern(regexp = ".+@mycompany\\.com$")
B. @Email(regexp = ".+@mycompany\\.com$")
C. @Email
@Pattern(regexp = ".+@mycompany\\.com$")
D. @Email
@Pattern(regexp = "^mycompany.com$")
Solution
Step 1: Use @Email for email format validation
@Email ensures the string is a valid email format regardless of domain.
Step 2: Use @Pattern to restrict domain
@Pattern with regex .+@mycompany\\.com$ ensures the email ends with '@mycompany.com'.
Step 3: Check other options
@Email(regexp = ".+@mycompany\\.com$") is invalid because @Email does not accept regexp attribute. @Pattern(regexp = ".+@mycompany\\.com$") misses email format check. @Email
@Pattern(regexp = "^mycompany.com$") regex is incorrect for email domain.
Final Answer:
@Email
@Pattern(regexp = ".+@mycompany\\.com$") -> Option C
Quick Check:
Combine @Email and @Pattern for domain restriction [OK]
Hint: Use @Email plus @Pattern with domain regex [OK]