0

Is there an annotation for springbote that makes it possible to only allow certain emails as email.

The email should only end with @ companyname.com

codecode
  • 11
  • 2
  • Not that I know of but, you can make your own annotation & validator logic in a new class like [here](https://stackoverflow.com/questions/35050936/jpa-validation-of-email-string-collection) – duppydodah Nov 29 '21 at 15:06
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 04 '21 at 05:00

1 Answers1

1

Your question is already answered by this thread

You can use the @Email constraint from javax.validation.constraint .

Per default the regular expression of the email constraint allows everything. So for you case following can be done:

@Email(regex = "\W*((?i)@companyname.com(?-i))") 
private String mail;

If you wished you can also modify the default constraint violation message.

Remember of course to add the needed dependency if not already provided e.g

implementation 'org.springframework.boot:spring-boot-starter-validation:2.4.0'
Andreas C.
  • 51
  • 3