Hibernate validator for chars

前端 未结 1 1876
一生所求
一生所求 2021-01-18 23:52

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 = \"custome         


        
相关标签:
1条回答
  • 2021-01-19 00:53

    Your should this regular expression for accepting only M or F.

    @Pattern(regexp = "^[M|F]{1}$", message ="Must be M or 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.

    You cannot use @Pattern for Character variable, You will get below exception.

    javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Character.

    0 讨论(0)
提交回复
热议问题