It must be a regular expression to accept only M or F.
@Pattern(regexp = "^[M|F]{1}$", message ="Must be M or F")
In your second use case as a character, you need to confirm that the character is "M" or "F". Another can be set as sex.
You cannot use @Pattern for the Character variable, you will get below exceptions.
javax.validation.UnexpectedTypeException: HV000030: the validator cannot be found for type: java.lang.Character.
source share