Validate positive integers

Looks like you are looking for natural numbers, I think you can use the regex pattern to get the desired output. Something like

@Pattern(regexp = "[\\s]*[0-9]*[1-9]+",message="msg")


Just use the annotation @Min in your bean:

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

If you use hibernate-validator then you may create a custom constraint which combines @Min and @Digits from the 3rd option by using @ConstraintComposition(AND). When you add @ReportAsSingleViolation, only a custom message will be shown.


Its better to use range annotation like below for positive numbers

@Range(min = 0l, message = "Please select positive numbers Only")

For negative numbers

@Range(min = -9223372036854775808l, max = 0l, message = "Please select Negative numbers Only")