Complete the code to validate an email field using the correct annotation.
public class User { @[1] private String email; }
The @Email annotation is used to validate that the string is a valid email address format.
Complete the code to validate a username with only letters and digits using @Pattern.
public class User { @Pattern(regexp = "[1]") private String username; }
The regex ^[a-zA-Z0-9]+$ ensures the username contains only letters and digits.
Fix the error in the @Pattern regex to allow emails with dots and dashes.
public class User { @Pattern(regexp = "[1]") private String email; }
This regex allows letters, digits, dots, underscores, percent, plus, and dash before the @, and dots and dashes in the domain part, ending with a valid TLD.
Fill both blanks to validate a password with at least 8 characters and only letters and digits.
public class User { @Pattern(regexp = "[1]") @Size(min = [2]) private String password; }
The regex ^[a-zA-Z0-9]+$ allows only letters and digits. The @Size(min = 8) ensures the password is at least 8 characters long.
Fill all three blanks to validate a phone number with exactly 10 digits using @Pattern and @Size.
public class User { @Pattern(regexp = "[1]") @Size(min = [2], max = [3]) private String phoneNumber; }
The regex ^\\d+$ ensures only digits. The @Size(min = 10, max = 10) ensures the phone number is exactly 10 digits long.