I want to allow only positive integers for number fields including zero. How can I define this validation using JSR 303 ?
I tried
@Min(value=0 message = "msg1")
- But it allows float values like 1.2.
@Digits(fraction = 0, integer = 10, message ="msg2")
- It accepts negative values.
@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!
If the Integer is greater than zero then it is a positive integer.
Python Code: n = float(input("Input a number: ")) if n >= 0: if n == 0: print("It is Zero! ") else: print("Number is Positive number. ") else: print("Number is Negative number. ")
If a number is greater than zero, it is a positive number. If a number is less than zero, it is a negative number. If a number equals to zero, it is zero.
Approach: We have used isNaN() function for validation of the textfield for numeric value only. Text-field data is passed in the function and if passed data is number then isNan() returns true and if data is not number or combination of both number and alphabets then it returns false.
Just use the annotation @Min
in your bean:
@Min(value = 0L, message = "The value must be positive") private Double value;
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With