How to parse numbers more strict than what NumberFormat does in Java?

后端 未结 7 1254
醉梦人生
醉梦人生 2021-02-09 01:59

I\'m validating user input from a form.

I parse the input with NumberFormat, but it is evil and allow almost anything. Is there any way to parse number more strict?

7条回答
  •  天命终不由人
    2021-02-09 02:16

    There are many ways to do that:

    • regex - check if it matches("\\d+")
    • with javax.validation - @Digits(fraction=0, integer=5)
    • apache commons IntegerValidator

提交回复
热议问题