Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Validating User Email and Username with @Email and @Pattern in Spring Boot
📖 Scenario: You are building a simple Spring Boot application to register users. You want to make sure the email addresses are valid and usernames follow specific rules.
🎯 Goal: Create a User class with fields email and username. Use @Email to validate the email format and @Pattern to ensure the username contains only letters and numbers, with length between 3 and 10 characters.
📋 What You'll Learn
Create a User class with email and username fields
Use @Email annotation on the email field
Use @Pattern annotation on the username field with regex ^[a-zA-Z0-9]{3,10}$
Include getter and setter methods for both fields
💡 Why This Matters
🌍 Real World
Validating user input like email and username is common in web applications to ensure data quality and security.
💼 Career
Understanding validation annotations in Spring Boot is essential for backend developers working on user registration and data validation.
Progress0 / 4 steps
1
Create the User class with email and username fields
Create a public class called User with two private String fields: email and username.
Spring Boot
Hint
Define the class and declare the two fields exactly as email and username.
2
Add @Email annotation to the email field
Add the @Email annotation above the email field in the User class. Also import jakarta.validation.constraints.Email.
Spring Boot
Hint
Place @Email directly above the email field.
3
Add @Pattern annotation to the username field
Add the @Pattern annotation above the username field with the regex "^[a-zA-Z0-9]{3,10}$". Import jakarta.validation.constraints.Pattern.
Spring Boot
Hint
Use the exact regex and place @Pattern above username.
4
Add getter and setter methods for email and username
Add public getter and setter methods for both email and username fields in the User class.
Spring Boot
Hint
Write standard getter and setter methods for both fields.
Practice
(1/5)
1. What is the primary purpose of the @Email annotation in Spring Boot validation?
easy
A. To convert a string to lowercase
B. To check if a string matches a custom regular expression
C. To ensure a string is not empty
D. To check if a string is a valid email format
Solution
Step 1: Understand the role of @Email
The @Email annotation is designed to validate that a string looks like a proper email address format.
Step 2: Compare with other annotations
@Pattern is used for custom regex, @NotEmpty ensures non-empty, and no annotation converts case automatically.
Final Answer:
To check if a string is a valid email format -> Option D
Quick Check:
@Email = Valid email format [OK]
Hint: Remember: @Email checks email format only [OK]
Common Mistakes:
Confusing @Email with @Pattern for custom regex
Thinking @Email checks if field is empty
Assuming @Email changes string content
2. Which of the following is the correct way to use @Pattern to allow only digits in a Spring Boot entity field?
easy
A. @Pattern(regex = "\\d+")
B. @Pattern(regexp = "\\d+")
C. @Pattern(pattern = "[0-9]*")
D. @Pattern(regexp = "[a-zA-Z]+")
Solution
Step 1: Check the correct attribute name
The correct attribute for @Pattern is regexp, not regex or pattern.
Step 2: Validate the regex for digits
The regex \\d+ matches one or more digits. @Pattern(regexp = "\\d+") uses correct syntax and regex.
Final Answer:
@Pattern(regexp = "\\d+") -> Option B
Quick Check:
@Pattern uses regexp attribute [OK]
Hint: Use regexp attribute, not regex or pattern [OK]
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]