0
0
Spring Bootframework~5 mins

@Email and @Pattern in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Email annotation in Spring Boot?
The @Email annotation checks if a string is a valid email address format. It helps ensure users enter proper emails in forms.
Click to reveal answer
beginner
How does the @Pattern annotation work in Spring Boot?
@Pattern validates a string against a regular expression (regex). It ensures the string matches a specific pattern you define.
Click to reveal answer
beginner
Example usage of @Email annotation in a Spring Boot model?
You add @Email(message = "Invalid email") above a String field to check email format. Example:<br>@Email(message = "Invalid email")<br>private String userEmail;
Click to reveal answer
intermediate
What happens if a string does not match the regex in @Pattern validation?
Spring Boot validation fails and returns an error message. You can customize the message with message = "Your message" in the annotation.
Click to reveal answer
intermediate
Can @Email and @Pattern be used together on the same field?
Yes, you can combine them to enforce email format and additional pattern rules. Both validations run and must pass for the input to be valid.
Click to reveal answer
What does the @Email annotation validate?
AIf a number is positive
BIf a string matches a regex pattern
CIf a string is a valid email format
DIf a field is not null
Which annotation uses a regular expression to validate a string?
A@Size
B@Email
C@NotNull
D@Pattern
How do you customize the error message shown when @Pattern validation fails?
ABy overriding <code>toString()</code>
BUsing <code>message</code> attribute in <code>@Pattern</code>
CUsing <code>@ErrorMessage</code> annotation
DYou cannot customize it
Can @Email annotation alone guarantee the email domain is real?
ANo, it only checks format
BYes, it checks domain existence
CYes, it sends a verification email
DNo, it checks only length
If you want to ensure a string contains only digits, which annotation is best?
A@Pattern(regexp = "\\d+")
B@Size(min=1)
C@NotNull
D@Email
Explain how you would use @Email and @Pattern annotations to validate a user input field in Spring Boot.
Think about combining format checks and pattern rules on the same field.
You got /4 concepts.
    What are the limitations of @Email validation and how can @Pattern help overcome them?
    Consider what @Email does not check and how regex can add control.
    You got /4 concepts.