Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex / javax.validation help -- verifying non-whitespace present -- Java

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?

like image 990
Eric Wilson Avatar asked Sep 10 '26 22:09

Eric Wilson


1 Answers

\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...)

like image 92
Ian Henry Avatar answered Sep 12 '26 11:09

Ian Henry