user2000189
user2000189

Reputation: 519

Java validation add constraint for only set of string values

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;

Upvotes: 0

Views: 2039

Answers (1)

Paco Abato
Paco Abato

Reputation: 4065

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

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

Upvotes: 2

Related Questions