I'm trying to validate some fields before persisting them to the database. In particular, I need to know that a String contains a non-whitespace character.
I'm using the javax.validation.constraints.Pattern annotation, as follows:
@Pattern(regexp = "[^\\s]")
private String field;
This seems to throw the ConstraintViolation on every String. What am I missing?
\S (or its equivalent [^\s]) should work according to the docs. Do you think it's putting an automatic "^" + expr + "$"? According to the docs it shouldn't, but maybe try .*\S.*
(from my comment -- thought this answer was a total shot in the dark; got lucky. Those docs could use some revising...)
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