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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With