如何用休眠验证数字字符串为数字? [英] How to validate number string as digit with hibernate?

查看:54
本文介绍了如何用休眠验证数字字符串为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在Hibernate Validation中使用哪些注释来验证字符串以应用于以下内容:

Which annotations would I have to use for Hibernate Validation to validate a String to apply to the following:

//should always have trimmed length = 6, only digits, only positive number
@NotEmpty
@Size(min = 6, max = 6)
public String getNumber { 
   return number.trim();
}

我如何应用数字验证?我要在这里使用@Digits(fraction = 0, integer = 6)吗?

How can I apply digit validation? Would I just use @Digits(fraction = 0, integer = 6) here?

推荐答案

您可以将所有约束替换为一个@Pattern(regexp="[\\d]{6}").这意味着一个长度为六的字符串,其中每个字符都是一个数字.

You could replace all your constraints with a single @Pattern(regexp="[\\d]{6}"). This would imply a string of length six where each character is a digit.

这篇关于如何用休眠验证数字字符串为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆