仅接受数字作为输入时使用哪种注释?

huangapple go评论66阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年4月4日 05:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61020485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定