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.
Jump into concepts and practice - no test required
@Email
@Pattern(regexp = "^[a-zA-Z0-9._%+-]+@example\\.com$")
private String email;| Step | Validation Annotation | Input Value | Validation Result | Action |
|---|---|---|---|---|
| 1 | user@example.com | Valid | Proceed to @Pattern | |
| 2 | @Pattern | user@example.com | Matches pattern | Accept input |
| 3 | user@wrongdomain.com | Valid | Proceed to @Pattern | |
| 4 | @Pattern | user@wrongdomain.com | Does not match pattern | Reject input |
| 5 | invalid-email | Invalid | Reject input |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | After Step 5 |
|---|---|---|---|---|---|---|
| null | "user@example.com" | "user@example.com" | "user@wrongdomain.com" | "user@wrongdomain.com" | "invalid-email" | |
| @Email Result | N/A | Valid | Valid | Valid | Valid | Invalid |
| @Pattern Result | N/A | N/A | Matches pattern | N/A | Does not match pattern | N/A |
| Final Validation | N/A | Pending | Accepted | Pending | Rejected | Rejected |
@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.
@Email annotation in Spring Boot validation?@Email annotation is designed to validate that a string looks like a proper email address format.@Pattern is used for custom regex, @NotEmpty ensures non-empty, and no annotation converts case automatically.@Pattern to allow only digits in a Spring Boot entity field?@Pattern is regexp, not regex or pattern.\\d+ matches one or more digits. @Pattern(regexp = "\\d+") uses correct syntax and regex.@Email @Pattern(regexp = ".+@example\\.com$") private String email;
user@test.com?@Email @Pattern(regexp = "[a-zA-Z]+") private String userEmail;
[a-zA-Z]+ allows only letters, no digits, dots, or @ symbols which are common in emails.mycompany.com domain. Which annotation setup is correct?.+@mycompany\\.com$ ensures the email ends with '@mycompany.com'.