字符的Hibernate验证器 [英] Hibernate validator for chars
问题描述
是否可以验证字符是M还是F,还是需要使用正则表达式的字符串?
Is it possible to validate that a Character is either M or F or do I need to use a string with a regex?
@Pattern(regexp = "^[MF]{1}$", message = "customer.sex.regex")
private String sex;
我想使用
I would like to use
private Character sex;
推荐答案
你应该这个正则表达式只接受M或F 。
Your should this regular expression for accepting only M or F.
@Pattern(regexp = "^[M|F]{1}$", message ="Must be M or F")
在第二种使用字符的情况下,您需要验证此字符是否M或F。其他可以设置为性别。
In your second case of using as Character, you need to validate that this character is whether "M" or "F". Other can be set as sex.
对于字符,您不能使用
@Pattern
$ b
You cannot use @Pattern
for Character
variable, You will get below exception.
javax.validation.UnexpectedTypeException:HV000030:没有验证器可以
$ b找到类型为:java.lang.Character。
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Character.
这篇关于字符的Hibernate验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!