0
0
Spring Bootframework~10 mins

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

Choose your learning style9 modes available
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.