How to check length 5 or 9 using sleep mode validator?

I can check the string length with annotation like:

@Length(max = 255) 

But what if I want to check that the length is 5 or 9? Can this be done with annotation?

+4
source share
2 answers

Here is the documentation for implementing the user restriction .

Simple failure:

  • You define your own annotation with the appropriate attributes
  • You define a class that will validate
  • You define verification messages.
  • You are using annotation

So maybe your annotation might look like this:

 @Constraint(validatedBy=YourChecker.class) //other annotations public @interface AllowedValues { int[] value(); } 
+3
source

Try @Pattern (regex = ". {5} |. {9}") (change the point to another character class if you don't want to match all.

+6
source

Source: https://habr.com/ru/post/1300465/


All Articles