Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate validator for chars

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;
like image 442
pethel Avatar asked Sep 26 '12 09:09

pethel


1 Answers

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.

like image 109
swemon Avatar answered Nov 05 '22 18:11

swemon