Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
Why does the following pattern validation not work
@Pattern(regexp = "^[a-zA-Z0-9_-]*$", message = "Only alpha-numeric characters are allowed")
  private String recipientId;

I want to allow only alpha-numeric characters but above validation does not work as i expect. It allows a request which has a plus sign (+) to pass through

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

When using brackets, hyphen is special character for defining range so you should escape it, if you want to match "-".

The regexp should looks like

[a-zA-Z0-9_\\-]*

Note : After testing it seems that you can use hyphen without escaping it when using brackets.

I would prefer this way (\\w|-)*

\w A word character: [a-zA-Z_0-9]


The issue is maybe not on the regexp but maybe on the way the validation annotation is used. You can use the annotation on the field directly or on the getter but not on both. Are you sure that you always follow the same coding pattern for all annotations?

Finally do you use the annotation @Valid to actually validate your input?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda