英文:
Which annotation to use for only accept number as input?
问题
下面的注册字段应只接受数字。我应该使用哪个注解?我进行了研究,人们在谈论 @Digits 和 @Pattern,但我不确定这是否是我需要的。
@Digits(integer=13, fraction=0)
private Long registration;
英文:
The registration field below should only accept number. Which annotation should I use? I made a research and people were talking about @Digits and @Pattern but I'm not sure that this is what I need
@Length(min=13, max=13)
private Long registration;
答案1
得分: 0
参数类型是Long,因此它只会接受数字,您甚至不需要进行验证。
您可以针对不同目的进行验证,如下所示。
如果它只应接受正数。
@Min(value = 0L, message = "值必须为正数")
private Long value;
或者使用正则表达式模式。
@Pattern(regexp = "[\\s]*[0-9]*[1-9]+", message = "消息")
英文:
The parameter type is Long so it will only accept numbers you don't even have to validate it.
You can validate for different purposes like below.
If it should accept only positive numbers.
@Min(value = 0L, message = "The value must be positive")
private Long value;
Or using regex pattern.
@Pattern(regexp = "[\\s]*[0-9]*[1-9]+",message="msg")
答案2
得分: 0
如果只允许正数,那么您可以使用@Positive或@PositiveOrZero。否则,@Pattern更适合覆盖所有验证。
英文:
If allowed only positive number then you can use either @Positive or @PositiveOrZero.
Or else @Pattern is better to cover all the validations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论