Bird
Raised Fist0
Spring Bootframework~10 mins

@Email and @Pattern in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - @Email and @Pattern
Start: Input String
Apply @Email Validation
Apply @Pattern
Accept Input
The input string is first checked by @Email for email format. If valid, @Pattern applies regex rules. Only if both pass, input is accepted.
Execution Sample
Spring Boot
@Email
@Pattern(regexp = "^[a-zA-Z0-9._%+-]+@example\\.com$")
private String email;
This code validates that the email field is a valid email and matches the pattern ending with @example.com.
Execution Table
StepValidation AnnotationInput ValueValidation ResultAction
1@Emailuser@example.comValidProceed to @Pattern
2@Patternuser@example.comMatches patternAccept input
3@Emailuser@wrongdomain.comValidProceed to @Pattern
4@Patternuser@wrongdomain.comDoes not match patternReject input
5@Emailinvalid-emailInvalidReject input
💡 Input is accepted only if both @Email and @Pattern validations pass.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
emailnull"user@example.com""user@example.com""user@wrongdomain.com""user@wrongdomain.com""invalid-email"
@Email ResultN/AValidValidValidValidInvalid
@Pattern ResultN/AN/AMatches patternN/ADoes not match patternN/A
Final ValidationN/APendingAcceptedPendingRejectedRejected
Key Moments - 2 Insights
Why does an email like 'user@wrongdomain.com' pass @Email but fail @Pattern?
Because @Email only checks if the string looks like an email format, but @Pattern enforces a specific domain pattern, as shown in execution_table rows 3 and 4.
What happens if the input is not a valid email format?
The validation stops at @Email and rejects the input immediately, as seen in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the @Email validation result for 'user@example.com' at step 1?
AValid
BInvalid
CPending
DRejected
💡 Hint
Check the 'Validation Result' column at step 1 in the execution_table.
At which step does the input 'user@wrongdomain.com' get rejected?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Look for 'Reject input' action for 'user@wrongdomain.com' in the execution_table.
If the @Pattern regex was changed to allow any domain, how would the validation result for 'user@wrongdomain.com' change at step 4?
AIt would still reject input
BIt would accept input
CIt would fail @Email validation
DNo change
💡 Hint
Refer to the 'Validation Result' and 'Action' columns at step 4 in the execution_table.
Concept Snapshot
@Email checks if a string is a valid email format.
@Pattern applies a regex to enforce specific rules.
Both validations must pass for acceptance.
Use @Email for general email format.
Use @Pattern for custom domain or format rules.
Full Transcript
This visual execution trace shows how Spring Boot validates an email field using @Email and @Pattern annotations. First, the input string is checked by @Email to confirm it looks like an email. If it passes, @Pattern applies a regular expression to enforce specific rules, such as domain restrictions. The execution table walks through examples: 'user@example.com' passes both validations and is accepted. 'user@wrongdomain.com' passes @Email but fails @Pattern and is rejected. An invalid email like 'invalid-email' fails @Email immediately and is rejected. The variable tracker shows how the input and validation results change step by step. Key moments clarify common confusions about why some inputs pass one validation but fail another. The quiz tests understanding of validation results and flow. This helps beginners see how these annotations work together to ensure correct email input.

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

  1. Step 1: Understand the role of @Email

    The @Email annotation is designed to validate that a string looks like a proper email address format.
  2. Step 2: Compare with other annotations

    @Pattern is used for custom regex, @NotEmpty ensures non-empty, and no annotation converts case automatically.
  3. Final Answer:

    To check if a string is a valid email format -> Option D
  4. 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

  1. Step 1: Check the correct attribute name

    The correct attribute for @Pattern is regexp, not regex or pattern.
  2. Step 2: Validate the regex for digits

    The regex \\d+ matches one or more digits. @Pattern(regexp = "\\d+") uses correct syntax and regex.
  3. Final Answer:

    @Pattern(regexp = "\\d+") -> Option B
  4. Quick Check:

    @Pattern uses regexp attribute [OK]
Hint: Use regexp attribute, not regex or pattern [OK]
Common Mistakes:
  • Using 'regex' instead of 'regexp' attribute
  • Using incorrect regex syntax
  • Confusing @Pattern attribute names
3. Given this Spring Boot entity field:
@Email
@Pattern(regexp = ".+@example\\.com$")
private String email;

What happens if the user inputs user@test.com?
medium
A. Validation fails because email does not end with @example.com
B. Validation passes because it is a valid email
C. Validation fails because @Email and @Pattern cannot be used together
D. Validation passes because @Pattern is ignored

Solution

  1. Step 1: Understand @Email validation

    @Email checks if the input looks like an email. 'user@test.com' is a valid email format, so it passes this check.
  2. Step 2: Understand @Pattern validation

    @Pattern requires the email to end with '@example.com'. 'user@test.com' ends with '@test.com', so it fails this pattern check.
  3. Final Answer:

    Validation fails because email does not end with @example.com -> Option A
  4. Quick Check:

    @Pattern restricts domain, so input fails [OK]
Hint: Both @Email and @Pattern must pass for validation [OK]
Common Mistakes:
  • Assuming @Email alone validates domain
  • Thinking @Pattern is ignored if @Email passes
  • Believing @Email and @Pattern conflict
4. Consider this code snippet in a Spring Boot entity:
@Email
@Pattern(regexp = "[a-zA-Z]+")
private String userEmail;

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

  1. Step 1: Analyze the regex pattern

    The regex [a-zA-Z]+ allows only letters, no digits, dots, or @ symbols which are common in emails.
  2. Step 2: Understand impact on email validation

    This pattern blocks valid email characters like digits, dots, and '@', causing valid emails to fail validation.
  3. Final Answer:

    Because the regex only allows letters, blocking digits and symbols in emails -> Option A
  4. 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

  1. Step 1: Use @Email for email format validation

    @Email ensures the string is a valid email format regardless of domain.
  2. Step 2: Use @Pattern to restrict domain

    @Pattern with regex .+@mycompany\\.com$ ensures the email ends with '@mycompany.com'.
  3. 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.
  4. Final Answer:

    @Email @Pattern(regexp = ".+@mycompany\\.com$") -> Option C
  5. Quick Check:

    Combine @Email and @Pattern for domain restriction [OK]
Hint: Use @Email plus @Pattern with domain regex [OK]
Common Mistakes:
  • Trying to put regexp inside @Email
  • Using incomplete regex that misses email format
  • Incorrect regex anchors for domain