0

Using java validation is it possible to add constrains for a string property like it should have only some set of values. (for ex : AA,BB,CC,DD). I know I can use something like below, but it does not stop some junk values apart from my expected values.

The below code, allow me junk/unexpected values

@Size(min=2, max=2,message="")
@pattern("regex patter to allow string only)
private String prop;
user2000189
  • 479
  • 4
  • 6
  • 22

1 Answers1

2
@Pattern(regexp = "word1|word2|word3")
String name;

If you need more elaborated regexp use the one you need in the usual way.

Paco Abato
  • 3,920
  • 4
  • 31
  • 54