Validate positive integers

xyz picture xyz · Feb 15, 2012 · Viewed 69.8k times · Source

I want to allow only positive integers for number fields including zero. How can I define this validation using JSR 303 ?
I tried

  1. @Min(value=0 message = "msg1") - But it allows float values like 1.2.

  2. @Digits(fraction = 0, integer = 10, message ="msg2") - It accepts negative values.

  3. @Min(value=0, message = "msg1" )
    @Digits(fraction = 0, integer = 10, message ="msg2") - It works fine but sometimes both the messages i.e. msg1 and msg2 are displayed.

Any suggestions?

Thanks!

Answer

Rodrigo Araujo picture Rodrigo Araujo · Jan 15, 2017

Just use the annotation @Min in your bean:

@Min(value = 0L, message = "The value must be positive")
private Double value;