Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda