I am using a @Pattern
for my @Email
validation to narrow the email addresses to just three domains and name.lastname
username. But my code fails and message is displayed every time. What am I doing wrong?
@Embeddable
public class Contact
{
@NotNull
@NotEmpty
private String firstname;
@NotNull
@NotEmpty
private String lastname;
@NotNull
@NotEmpty
@Email
@Pattern.List({ @Pattern(regexp = ".+(@domain1.com|@domain-2.com|@dom-ain-three.com)", message = "Wrong email address") })
private String email;
}
The correct code is:
@Embeddable
public class Contact
{
@NotNull
@NotEmpty
private String firstname;
@NotNull
@NotEmpty
private String lastname;
@NotNull
@NotEmpty
@Email
@Pattern.List({ @Pattern(regexp = ".+((@domain1\\.com)|(@domain-2\\.com)|(@dom-ain-three\\.com))", message = "Wrong email address") })
private String email;
}
Escaping the dot characters (meaning any character except new line) fixed the regex.